X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mono%2Fmini%2Faot-runtime.c;h=7f02b7593c66057a462593390cbf3ab581b6a34f;hb=07178b9c886ac087f6785d3fc593bfa98d169082;hp=c25f8e8740812334be6b5beb7b3862c264868804;hpb=b54ec387b98d61d32cb2427cef4ca1a7302dd968;p=mono.git diff --git a/mono/mini/aot-runtime.c b/mono/mini/aot-runtime.c index c25f8e87408..7f02b7593c6 100644 --- a/mono/mini/aot-runtime.c +++ b/mono/mini/aot-runtime.c @@ -19,7 +19,7 @@ #include #endif -#if PLATFORM_WIN32 +#if HOST_WIN32 #include #include #endif @@ -35,6 +35,10 @@ #include /* for WIFEXITED, WEXITSTATUS */ #endif +#ifdef HAVE_DL_ITERATE_PHDR +#include +#endif + #include #include #include @@ -57,7 +61,7 @@ #ifndef DISABLE_AOT -#ifdef PLATFORM_WIN32 +#ifdef TARGET_WIN32 #define SHARED_EXT ".dll" #elif ((defined(__ppc__) || defined(__powerpc__) || defined(__ppc64__)) || defined(__MACH__)) && !defined(__linux__) #define SHARED_EXT ".dylib" @@ -70,8 +74,6 @@ typedef struct MonoAotModule { char *aot_name; - /* Optimization flags used to compile the module */ - guint32 opts; /* Pointer to the Global Offset Table */ gpointer *got; GHashTable *name_cache; @@ -93,24 +95,28 @@ typedef struct MonoAotModule { guint8 *code_end; guint8 *plt; guint8 *plt_end; - guint32 *code_offsets; - guint8 *method_info; + guint8 *blob; + gint32 *code_offsets; + /* This contains pairs sorted by offset */ + /* This is needed because LLVM emitted methods can be in any order */ + gint32 *sorted_code_offsets; guint32 *method_info_offsets; - guint8 *got_info; guint32 *got_info_offsets; - guint8 *ex_info; guint32 *ex_info_offsets; - guint32 *method_order; - guint32 *method_order_end; - guint8 *class_info; guint32 *class_info_offsets; guint32 *methods_loaded; guint16 *class_name_table; guint32 *extra_method_table; guint32 *extra_method_info_offsets; - guint8 *extra_method_info; guint8 *unwind_info; + /* Points to the GNU .eh_frame_hdr section, if it exists */ + guint8 *eh_frame_hdr; + + /* Points to the .ARM.exidx section, if it exists */ + guint8 *arm_exidx; + guint32 arm_exidx_size; + /* Points to the trampolines */ guint8 *trampolines [MONO_AOT_TRAMP_NUM]; /* The first unused trampoline of each kind */ @@ -156,6 +162,7 @@ static gint32 mono_last_aot_method = -1; static gboolean make_unreadable = FALSE; static guint32 name_table_accesses = 0; +static guint32 n_pagefaults = 0; /* Used to speed-up find_aot_module () */ static gsize aot_code_low_addr = (gssize)-1; @@ -168,37 +175,48 @@ init_plt (MonoAotModule *info); /* AOT RUNTIME */ /*****************************************************/ +/* + * load_image: + * + * Load one of the images referenced by AMODULE. Returns NULL if the image is not + * found, and sets the loader error if SET_ERROR is TRUE. + */ static MonoImage * -load_image (MonoAotModule *module, int index) +load_image (MonoAotModule *amodule, int index, gboolean set_error) { MonoAssembly *assembly; MonoImageOpenStatus status; - g_assert (index < module->image_table_len); + g_assert (index < amodule->image_table_len); - if (module->image_table [index]) - return module->image_table [index]; - if (module->out_of_date) + if (amodule->image_table [index]) + return amodule->image_table [index]; + if (amodule->out_of_date) return NULL; - assembly = mono_assembly_load (&module->image_names [index], NULL, &status); + assembly = mono_assembly_load (&amodule->image_names [index], NULL, &status); if (!assembly) { - mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is unusable because dependency %s is not found.\n", module->aot_name, module->image_names [index].name); - module->out_of_date = TRUE; + mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is unusable because dependency %s is not found.\n", amodule->aot_name, amodule->image_names [index].name); + amodule->out_of_date = TRUE; + + if (set_error) { + char *full_name = mono_stringify_assembly_name (&amodule->image_names [index]); + mono_loader_set_error_assembly_load (full_name, FALSE); + g_free (full_name); + } return NULL; } - if (strcmp (assembly->image->guid, module->image_guids [index])) { - mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date (Older than dependency %s).\n", module->aot_name, module->image_names [index].name); - module->out_of_date = TRUE; + if (strcmp (assembly->image->guid, amodule->image_guids [index])) { + mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date (Older than dependency %s).\n", amodule->aot_name, amodule->image_names [index].name); + amodule->out_of_date = TRUE; return NULL; } - module->image_table [index] = assembly->image; + amodule->image_table [index] = assembly->image; return assembly->image; } - static inline gint32 decode_value (guint8 *ptr, guint8 **rptr) { @@ -229,6 +247,48 @@ decode_value (guint8 *ptr, guint8 **rptr) return len; } +/* + * mono_aot_get_method: + * + * Decode an offset table emitted by emit_offset_table (), returning the INDEXth + * entry. + */ +static guint32 +mono_aot_get_offset (guint32 *table, int index) +{ + int i, group, ngroups, index_entry_size; + int start_offset, offset, noffsets, group_size; + guint8 *data_start, *p; + guint32 *index32 = NULL; + guint16 *index16 = NULL; + + noffsets = table [0]; + group_size = table [1]; + ngroups = table [2]; + index_entry_size = table [3]; + group = index / group_size; + + if (index_entry_size == 2) { + index16 = (guint16*)&table [4]; + data_start = (guint8*)&index16 [ngroups]; + p = data_start + index16 [group]; + } else { + index32 = (guint32*)&table [4]; + data_start = (guint8*)&index32 [ngroups]; + p = data_start + index32 [group]; + } + + /* offset will contain the value of offsets [group * group_size] */ + offset = start_offset = decode_value (p, &p); + for (i = group * group_size + 1; i <= index; ++i) { + offset += decode_value (p, &p); + } + + //printf ("Offset lookup: %d -> %d, start=%d, p=%d\n", index, offset, start_offset, table [3 + group]); + + return offset; +} + static MonoMethod* decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf); @@ -300,7 +360,7 @@ decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf) return NULL; } if (mono_metadata_token_table (token) == 0) { - image = load_image (module, decode_value (p, &p)); + image = load_image (module, decode_value (p, &p), TRUE); if (!image) return NULL; klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token); @@ -314,6 +374,8 @@ decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf) MonoType *type; gclass = decode_klass_ref (module, p, &p); + if (!gclass) + return NULL; g_assert (gclass->generic_container); memset (&ctx, 0, sizeof (ctx)); @@ -364,14 +426,14 @@ decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf) g_assert_not_reached (); } } else { - image = load_image (module, decode_value (p, &p)); + image = load_image (module, decode_value (p, &p), TRUE); if (!image) return NULL; klass = mono_class_get (image, token); } } else if (token == MONO_TOKEN_TYPE_DEF) { /* Array */ - image = load_image (module, decode_value (p, &p)); + image = load_image (module, decode_value (p, &p), TRUE); if (!image) return NULL; rank = decode_value (p, &p); @@ -381,7 +443,6 @@ decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf) g_assert_not_reached (); } g_assert (klass); - mono_class_init (klass); *endbuf = p; return klass; @@ -526,6 +587,9 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g *method = mono_gc_get_managed_allocator_by_type (atype); break; } + case MONO_WRAPPER_WRITE_BARRIER: + *method = mono_gc_get_write_barrier (); + break; case MONO_WRAPPER_STELEMREF: *method = mono_marshal_get_stelemref (); break; @@ -563,6 +627,19 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g *method = mono_marshal_get_runtime_invoke (m, FALSE); break; } + case MONO_WRAPPER_MANAGED_TO_MANAGED: { + int subtype = decode_value (p, &p); + + if (subtype == MONO_AOT_WRAPPER_ELEMENT_ADDR) { + int rank = decode_value (p, &p); + int elem_size = decode_value (p, &p); + + *method = mono_marshal_get_array_address (rank, elem_size); + } else { + g_assert_not_reached (); + } + break; + } default: g_assert_not_reached (); } @@ -573,7 +650,7 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g image_index = decode_value (p, &p); *token = decode_value (p, &p); - image = load_image (module, image_index); + image = load_image (module, image_index, TRUE); if (!image) return NULL; } else if (image_index == MONO_AOT_METHODREF_GINST) { @@ -591,7 +668,7 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g image_index = decode_value (p, &p); *token = decode_value (p, &p); - image = load_image (module, image_index); + image = load_image (module, image_index, TRUE); if (!image) return NULL; @@ -646,7 +723,7 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g g_assert (image_index < MONO_AOT_METHODREF_MIN); *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff); - image = load_image (module, image_index); + image = load_image (module, image_index, TRUE); if (!image) return NULL; } @@ -707,7 +784,7 @@ create_cache_structure (void) tmp = g_build_filename (home, ".mono", NULL); if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) { mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp); -#ifdef PLATFORM_WIN32 +#ifdef HOST_WIN32 err = mkdir (tmp); #else err = mkdir (tmp, 0777); @@ -722,7 +799,7 @@ create_cache_structure (void) tmp = g_build_filename (home, ".mono", "aot-cache", NULL); if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) { mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp); -#ifdef PLATFORM_WIN32 +#ifdef HOST_WIN32 err = mkdir (tmp); #else err = mkdir (tmp, 0777); @@ -791,7 +868,7 @@ load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name) res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL); -#if !defined(PLATFORM_WIN32) && !defined(__ppc__) && !defined(__ppc64__) && !defined(__powerpc__) +#if !defined(HOST_WIN32) && !defined(__ppc__) && !defined(__ppc64__) && !defined(__powerpc__) if (res) { if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0)) mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err); @@ -823,20 +900,78 @@ static void find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value) { if (globals) { - int i = 0; + int global_index; + guint16 *table, *entry; + guint16 table_size; + guint32 hash; + + /* The first entry points to the hash */ + table = globals [0]; + globals ++; + + table_size = table [0]; + table ++; + + hash = mono_metadata_str_hash (name) % table_size; - *value = NULL; - for (i = 0; globals [i]; i+= 2) { - if (strcmp (globals [i], name) == 0) { - *value = globals [i + 1]; + entry = &table [hash * 2]; + + /* Search the hash for the index into the globals table */ + global_index = -1; + while (entry [0] != 0) { + guint32 index = entry [0] - 1; + guint32 next = entry [1]; + + //printf ("X: %s %s\n", (char*)globals [index * 2], name); + + if (!strcmp (globals [index * 2], name)) { + global_index = index; + break; + } + + if (next != 0) { + entry = &table [next * 2]; + } else { break; } } + + if (global_index != -1) + *value = globals [global_index * 2 + 1]; + else + *value = NULL; } else { mono_dl_symbol (module, name, value); } } +#ifndef SHT_ARM_EXIDX +#define SHT_ARM_EXIDX 0x70000001 +#endif + +#ifdef HAVE_DL_ITERATE_PHDR +static int +dl_callback (struct dl_phdr_info *info, size_t size, void *data) +{ + int j; + MonoAotModule *amodule = data; + + if (!strcmp (amodule->aot_name, info->dlpi_name)) { + for (j = 0; j < info->dlpi_phnum; j++) { + if (info->dlpi_phdr [j].p_type == PT_GNU_EH_FRAME) + amodule->eh_frame_hdr = (guint8*)(info->dlpi_addr + info->dlpi_phdr [j].p_vaddr); + if (info->dlpi_phdr [j].p_type == SHT_ARM_EXIDX) { + amodule->arm_exidx = (guint8*)(info->dlpi_addr + info->dlpi_phdr [j].p_vaddr); + amodule->arm_exidx_size = info->dlpi_phdr [j].p_filesz; + } + } + return 1; + } else { + return 0; + } +} +#endif + static void load_aot_module (MonoAssembly *assembly, gpointer user_data) { @@ -931,14 +1066,10 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data) } g_free (build_info); - { - char *full_aot_str; - - find_symbol (sofile, globals, "mono_aot_full_aot", (gpointer *)&full_aot_str); + find_symbol (sofile, globals, "mono_aot_file_info", (gpointer*)&file_info); + g_assert (file_info); - if (full_aot_str && !strcmp (full_aot_str, "TRUE")) - full_aot = TRUE; - } + full_aot = ((MonoAotFileInfo*)file_info)->flags & MONO_AOT_FILE_FLAG_FULL_AOT; if (mono_aot_only && !full_aot) { fprintf (stderr, "Can't use AOT image '%s' in aot-only mode because it is not compiled with --aot=full.\n", aot_name); @@ -949,6 +1080,11 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data) usable = FALSE; } + if ((((MonoAotFileInfo*)file_info)->flags & MONO_AOT_FILE_FLAG_WITH_LLVM) && !mono_use_llvm) { + mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled with LLVM.\n", aot_name); + usable = FALSE; + } + if (!usable) { if (mono_aot_only) { fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode.\n", aot_name); @@ -961,9 +1097,6 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data) return; } - 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; @@ -976,8 +1109,6 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data) amodule->sofile = sofile; amodule->method_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL); - sscanf (opt_flags, "%d", &amodule->opts); - /* Read image table */ { guint32 table_len, i; @@ -1020,22 +1151,16 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data) } /* Read method and method_info tables */ - find_symbol (sofile, globals, "method_offsets", (gpointer*)&amodule->code_offsets); + find_symbol (sofile, globals, "code_offsets", (gpointer*)&amodule->code_offsets); find_symbol (sofile, globals, "methods", (gpointer*)&amodule->code); find_symbol (sofile, globals, "methods_end", (gpointer*)&amodule->code_end); find_symbol (sofile, globals, "method_info_offsets", (gpointer*)&amodule->method_info_offsets); - find_symbol (sofile, globals, "method_info", (gpointer*)&amodule->method_info); find_symbol (sofile, globals, "ex_info_offsets", (gpointer*)&amodule->ex_info_offsets); - find_symbol (sofile, globals, "ex_info", (gpointer*)&amodule->ex_info); - find_symbol (sofile, globals, "method_order", (gpointer*)&amodule->method_order); - find_symbol (sofile, globals, "method_order_end", (gpointer*)&amodule->method_order_end); - find_symbol (sofile, globals, "class_info", (gpointer*)&amodule->class_info); + find_symbol (sofile, globals, "blob", (gpointer*)&amodule->blob); find_symbol (sofile, globals, "class_info_offsets", (gpointer*)&amodule->class_info_offsets); find_symbol (sofile, globals, "class_name_table", (gpointer *)&amodule->class_name_table); find_symbol (sofile, globals, "extra_method_table", (gpointer *)&amodule->extra_method_table); - find_symbol (sofile, globals, "extra_method_info", (gpointer *)&amodule->extra_method_info); find_symbol (sofile, globals, "extra_method_info_offsets", (gpointer *)&amodule->extra_method_info_offsets); - find_symbol (sofile, globals, "got_info", (gpointer*)&amodule->got_info); find_symbol (sofile, globals, "got_info_offsets", (gpointer*)&amodule->got_info_offsets); find_symbol (sofile, globals, "specific_trampolines", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_SPECIFIC])); find_symbol (sofile, globals, "static_rgctx_trampolines", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_STATIC_RGCTX])); @@ -1049,19 +1174,21 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data) find_symbol (sofile, globals, "plt_end", (gpointer*)&amodule->plt_end); if (make_unreadable) { -#ifndef PLATFORM_WIN32 +#ifndef TARGET_WIN32 guint8 *addr; - guint8 *page_start; - int pages, err, len; + guint8 *page_start, *page_end; + int err, len; addr = amodule->mem_begin; len = amodule->mem_end - amodule->mem_begin; /* Round down in both directions to avoid modifying data which is not ours */ page_start = (guint8 *) (((gssize) (addr)) & ~ (mono_pagesize () - 1)) + mono_pagesize (); - pages = ((addr + len - page_start + mono_pagesize () - 1) / mono_pagesize ()) - 1; - err = mono_mprotect (page_start, pages * mono_pagesize (), MONO_MMAP_NONE); - g_assert (err == 0); + page_end = (guint8 *) (((gssize) (addr + len)) & ~ (mono_pagesize () - 1)); + if (page_end > page_start) { + err = mono_mprotect (page_start, (page_end - page_start), MONO_MMAP_NONE); + g_assert (err == 0); + } #endif } @@ -1076,6 +1203,11 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data) mono_jit_info_add_aot_module (assembly->image, amodule->code, amodule->code_end); assembly->image->aot_module = amodule; + +#ifdef HAVE_DL_ITERATE_PHDR + /* Lookup the address of the .eh_frame_hdr () section if available */ + dl_iterate_phdr (dl_callback, amodule); +#endif if (mono_aot_only) { if (mono_defaults.corlib) { @@ -1093,9 +1225,10 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data) * referenced assemblies, we depend on the exact versions of the referenced assemblies. * MS calls this 'hard binding'. This means we have to load all referenced assemblies * non-lazily, since we can't handle out-of-date errors later. + * The cached class info also depends on the exact assemblies. */ for (i = 0; i < amodule->image_table_len; ++i) - load_image (amodule, i); + load_image (amodule, i, FALSE); if (amodule->out_of_date) { mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT Module %s is unusable because a dependency is out-of-date.\n", assembly->image->name); @@ -1185,6 +1318,7 @@ decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guin info->has_references = (flags >> 5) & 0x1; info->has_static_refs = (flags >> 6) & 0x1; info->no_special_static_fields = (flags >> 7) & 0x1; + info->is_generic_container = (flags >> 8) & 0x1; if (info->has_cctor) { MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, NULL, NULL, buf, &buf); @@ -1223,7 +1357,7 @@ mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int sl if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !aot_module) return NULL; - info = &aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]]; + info = &aot_module->blob [mono_aot_get_offset (aot_module->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)]; p = info; err = decode_cached_class_info (aot_module, &class_info, p, &p); @@ -1255,7 +1389,7 @@ mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res) if (klass->rank || !aot_module) return FALSE; - p = (guint8*)&aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]]; + p = (guint8*)&aot_module->blob [mono_aot_get_offset (aot_module->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)]; err = decode_cached_class_info (aot_module, res, p, &p); if (!err) @@ -1320,7 +1454,7 @@ mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const ch full_name = g_strdup_printf ("%s.%s", name_space, name); } } - hash = mono_aot_str_hash (full_name) % table_size; + hash = mono_metadata_str_hash (full_name) % table_size; if (full_name != full_name_buf) g_free (full_name); @@ -1372,28 +1506,416 @@ mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const ch return TRUE; } +#define DW_EH_PE_omit 0xff +#define DW_EH_PE_uleb128 0x01 +#define DW_EH_PE_udata2 0x02 +#define DW_EH_PE_udata4 0x03 +#define DW_EH_PE_udata8 0x04 +#define DW_EH_PE_sleb128 0x09 +#define DW_EH_PE_sdata2 0x0A +#define DW_EH_PE_sdata4 0x0B +#define DW_EH_PE_sdata8 0x0C + +#define DW_EH_PE_absptr 0x00 +#define DW_EH_PE_pcrel 0x10 +#define DW_EH_PE_datarel 0x30 +#define DW_EH_PE_omit 0xff + +typedef struct +{ + guint8 version; + guint8 eh_frame_ptr_enc; + guint8 fde_count_enc; + guint8 table_enc; + guint8 rest; +} eh_frame_hdr; + +/* + * decode_eh_frame: + * + * Decode the exception handling information in the .eh_frame section of the AOT + * file belong to CODE, and construct a MonoJitInfo structure from it. + * LOCKING: Acquires the domain lock. + */ +static G_GNUC_UNUSED void +decode_eh_frame (MonoAotModule *amodule, MonoDomain *domain, + MonoMethod *method, guint8 *code, MonoJitInfo *jinfo) +{ + eh_frame_hdr *hdr; + guint8 *p; + guint8 *eh_frame, *unwind_info; + guint32 eh_frame_ptr; + int fde_count; + gint32 *table; + int i, pos, left, right, offset, offset1, offset2; + guint32 unw_len, code_len; + MonoJitExceptionInfo *ei; + guint32 ei_len; + + g_assert (amodule->eh_frame_hdr); + + // http://refspecs.freestandards.org/LSB_1.3.0/gLSB/gLSB/ehframehdr.html + hdr = (eh_frame_hdr*)amodule->eh_frame_hdr; + g_assert (hdr->version == 1); + g_assert (hdr->eh_frame_ptr_enc == (DW_EH_PE_pcrel | DW_EH_PE_sdata4)); + g_assert (hdr->fde_count_enc == DW_EH_PE_udata4); + g_assert (hdr->table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4)); + + p = &(hdr->rest); + eh_frame_ptr = *(guint32*)p; + p += 4; + fde_count = *(guint32*)p; + p += 4; + table = (gint32*)p; + + /* Binary search in the table to find the entry for code */ + offset = code - amodule->eh_frame_hdr; + + left = 0; + right = fde_count; + while (TRUE) { + pos = (left + right) / 2; + + offset1 = table [(pos * 2)]; + if (pos + 1 == fde_count) + /* FIXME: */ + offset2 = amodule->code_end - amodule->code; + else + offset2 = table [(pos + 1) * 2]; + + if (offset < offset1) + right = pos; + else if (offset >= offset2) + left = pos + 1; + else + break; + } + + g_assert (code >= amodule->eh_frame_hdr + table [(pos * 2)]); + if (pos < fde_count) + g_assert (code < amodule->eh_frame_hdr + table [(pos * 2) + 2]); + + eh_frame = amodule->eh_frame_hdr + table [(pos * 2) + 1]; + + unwind_info = mono_unwind_decode_fde (eh_frame, &unw_len, &code_len, &ei, &ei_len, NULL); + + jinfo->code_size = code_len; + jinfo->used_regs = mono_cache_unwind_info (unwind_info, unw_len); + jinfo->method = method; + jinfo->code_start = code; + jinfo->domain_neutral = 0; + /* This signals that used_regs points to a normal cached unwind info */ + jinfo->from_aot = 0; + + g_assert (ei_len == jinfo->num_clauses); + for (i = 0; i < jinfo->num_clauses; ++i) { + MonoJitExceptionInfo *jei = &jinfo->clauses [i]; + + jei->try_start = ei [i].try_start; + jei->try_end = ei [i].try_end; + jei->handler_start = ei [i].handler_start; + } +} + +#ifdef TARGET_ARM + +/* The offsets in the table are 31 bits long, have to extend them to 32 */ +#define EXTEND_PREL31(val) ((((gint32)(val)) << 1) >> 1) + +static inline guint32 +decode_uleb128 (guint8 *buf, guint8 **endbuf) +{ + guint8 *p = buf; + guint32 res = 0; + int shift = 0; + + while (TRUE) { + guint8 b = *p; + p ++; + + res = res | (((int)(b & 0x7f)) << shift); + if (!(b & 0x80)) + break; + shift += 7; + } + + *endbuf = p; + + return res; +} + +static GSList* +decode_arm_eh_ops (guint8 *unwind_ops, int nops) +{ + int i, vsp_reg, vsp_offset; + GSList *ops; + gint32 *reg_offsets; + + /* + * Have to convert the ARM unwind info into DWARF unwind info. + * The ARM unwind info specifies a simple set of instructions which need to be + * executed during unwinding. It manipulates a virtual stack pointer (vsp). The + * connection with DWARF unwind info is the following: after all ARM unwind + * opcodes have been executed, the stack should be completely unwound, i.e. + * vsp == DWARF CFA. This allows us to construct the DWARF opcodes corresponding + * to the ARM opcodes. + * The ARM unwind info is not instruction precise, i. e. it can't handle + * async exceptions etc. + */ + /* The reg used to compute the initial value of vsp */ + vsp_reg = ARMREG_SP; + /* The offset between vsp_reg and the CFA */ + vsp_offset = 0; + + /* The register save offsets from the initial value of vsp */ + reg_offsets = g_new0 (gint32, 16); + for (i = 0; i < 16; ++i) + reg_offsets [i] = -1; + + /* section 9.3 in the ehabi doc */ + for (i = 0; i < nops; ++i) { + guint8 op = unwind_ops [i]; + + if ((op >> 6) == 0) { + /* vsp = vsp + (xxxxxx << 2) + 4. */ + vsp_offset += ((op & 0x3f) << 2) + 4; + } else if ((op >> 6) == 1) { + /* vsp = vsp - (xxxxxx << 2) - 4. */ + vsp_offset -= ((op & 0x3f) << 2) + 4; + } else if (op == 0xb2) { + /* vsp = vsp = vsp + 0x204 + (uleb128 << 2) */ + guint8 *p = unwind_ops + i + 1; + guint32 v = decode_uleb128 (p, &p); + + vsp_offset += 0x204 + (v << 2); + i = (p - unwind_ops) - 1; + } else if (op >= 0x80 && op <= 0x8f) { + /* pop registers */ + guint8 op2; + GSList *regs; + int j; + + g_assert (i + 1 < nops); + op2 = unwind_ops [i + 1]; + + regs = NULL; + for (j = 0; j < 8; ++j) + if (op2 & (0x1 << j)) + regs = g_slist_append (regs, GUINT_TO_POINTER (ARMREG_R4 + j)); + for (j = 0; j < 4; ++j) + if (op & (0x1 << j)) + regs = g_slist_append (regs, GUINT_TO_POINTER (ARMREG_R12 + j)); + g_assert (regs); + + for (j = 0; j < g_slist_length (regs); ++j) + reg_offsets [GPOINTER_TO_UINT (g_slist_nth (regs, j)->data)] = vsp_offset + (j * 4); + + vsp_offset += g_slist_length (regs) * 4; + + g_slist_free (regs); + + i ++; + } else if (op >= 0xa8 && op <= 0xaf) { + GSList *regs; + int j; + + /* pop r4-r[4 + nnn], r14 */ + + regs = NULL; + for (j = 0; j <= (op & 0x7); ++j) + regs = g_slist_append (regs, GUINT_TO_POINTER (ARMREG_R4 + j)); + regs = g_slist_append (regs, GUINT_TO_POINTER (ARMREG_R14)); + + for (j = 0; j < g_slist_length (regs); ++j) + reg_offsets [GPOINTER_TO_UINT (g_slist_nth (regs, j)->data)] = vsp_offset + (j * 4); + + vsp_offset += g_slist_length (regs) * 4; + + g_slist_free (regs); + } else if (op == 0xb0) { + /* finish */ + break; + } else if (op >= 0x90 && op <= 0x9f && op != 0x9d && op != 0x9f) { + /* vsp = */ + vsp_reg = op & 0xf; + vsp_offset = 0; + } else { + int j; + + for (j = 0; j < nops; ++j) + printf ("%x ", unwind_ops [j]); + printf (" / %d\n", i); + g_assert_not_reached (); + } + } + + ops = NULL; + + /* vsp_reg + vsp_offset = CFA */ + mono_add_unwind_op_def_cfa (ops, (guint8*)NULL, (guint8*)NULL, vsp_reg, vsp_offset); + + for (i = 0; i < 16; ++i) { + if (reg_offsets [i] != -1) + /* The reg is saved at vsp_reg + reg_offset [i] == CFA - (vsp_offset - reg_offset [i]) */ + mono_add_unwind_op_offset (ops, (guint8*)NULL, (guint8*)NULL, i, - (vsp_offset - reg_offsets [i])); + } + + return ops; +} + +/* + * decode_arm_exidx: + * + * Decode the exception handling information in the .ARM.exidx section of the AOT + * file belong to CODE, and construct a MonoJitInfo structure from it. + * LOCKING: Acquires the domain lock. + */ +static void +decode_arm_exidx (MonoAotModule *amodule, MonoDomain *domain, + MonoMethod *method, guint8 *code, guint32 code_len, MonoJitInfo *jinfo) +{ + guint32 *table; + guint8 *base, *code1, *code2; + int i, pos, left, right, offset, offset1, offset2, count, nwords, nops; + guint32 entry; + guint8 unwind_ops [64]; + GSList *ops; + guint8 *unwind_info; + guint32 unw_len; + + g_assert (amodule->arm_exidx); + + table = (guint32*)amodule->arm_exidx; + + /* + * The table format is described in: + * infocenter.arm.com/help/topic/com.arm.doc.../IHI0038A_ehabi.pdf + */ + + base = amodule->arm_exidx; + count = amodule->arm_exidx_size / 8; + + /* Binary search in the table to find the entry for code */ + offset = code - base; + + left = 0; + right = count; + while (TRUE) { + pos = (left + right) / 2; + + if (left == right) + break; + + offset1 = EXTEND_PREL31 (table [(pos * 2)]); + code1 = (guint8*)&(table [pos * 2]) + offset1; + if (pos + 1 == count) + /* FIXME: */ + offset2 = amodule->code_end - amodule->code; + else + offset2 = EXTEND_PREL31 (table [(pos + 1) * 2]); + code2 = (guint8*)&(table [(pos + 1) * 2]) + offset2; + + if (code < code1) + right = pos; + else if (code >= code2) + left = pos + 1; + else + break; + } + + if (code >= code1) { + /* + * The linker might merge duplicate unwind table entries, so + * offset1 and offset2 might point to another method, but this is not a problem. + */ + code1 = (guint8*)&(table [pos * 2]) + offset1; + code2 = (guint8*)&(table [(pos + 1) * 2]) + offset2; + + g_assert (code >= code1); + if (pos < count) + g_assert (code < code2); + + entry = table [(pos * 2) + 1]; + + /* inline entry, compact model, personality routine 0 */ + if ((entry & 0xff000000) == 0x80000000) { + nops = 3; + unwind_ops [0] = (entry & 0x00ff0000) >> 16; + unwind_ops [1] = (entry & 0x0000ff00) >> 8; + unwind_ops [2] = (entry & 0x000000ff) >> 0; + + ops = decode_arm_eh_ops (unwind_ops, nops); + } else if ((entry & 0x80000000) == 0) { + /* non-inline entry */ + guint8 *data = (guint8*)&table [(pos * 2) + 1] + EXTEND_PREL31 (entry); + + entry = ((guint32*)data) [0]; + + /* compact model, personality routine 1 */ + g_assert ((entry & 0xff000000) == 0x81000000); + + nwords = (entry & 0x00ff0000) >> 16; + nops = nwords * 4 + 2; + g_assert (nops < 64); + + unwind_ops [0] = (entry & 0x0000ff00) >> 8; + unwind_ops [1] = (entry & 0x000000ff) >> 0; + + for (i = 0; i < nwords; ++i) { + entry = ((guint32*)data) [1 + i]; + unwind_ops [(i * 4) + 2] = (entry & 0xff000000) >> 24; + unwind_ops [(i * 4) + 2 + 1] = (entry & 0x00ff0000) >> 16; + unwind_ops [(i * 4) + 2 + 2] = (entry & 0x0000ff00) >> 8; + unwind_ops [(i * 4) + 2 + 3] = (entry & 0x000000ff) >> 0; + } + + ops = decode_arm_eh_ops (unwind_ops, nops); + } else { + NOT_IMPLEMENTED; + } + + unwind_info = mono_unwind_ops_encode (ops, &unw_len); + } else { + /* The method has no unwind info */ + unwind_info = NULL; + unw_len = 0; + } + + jinfo->code_size = code_len; + jinfo->used_regs = mono_cache_unwind_info (unwind_info, unw_len); + jinfo->method = method; + jinfo->code_start = code; + jinfo->domain_neutral = 0; + /* This signals that used_regs points to a normal cached unwind info */ + jinfo->from_aot = 0; +} +#endif + /* * LOCKING: Acquires the domain lock. */ static MonoJitInfo* decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain, - MonoMethod *method, guint8* ex_info, guint8 *code) + MonoMethod *method, guint8* ex_info, guint8 *addr, + guint8 *code, guint32 code_len) { int i, buf_len; MonoJitInfo *jinfo; - guint code_len, used_int_regs, flags; - gboolean has_generic_jit_info, has_dwarf_unwind_info, has_clauses; + guint used_int_regs, flags; + gboolean has_generic_jit_info, has_dwarf_unwind_info, has_clauses, has_seq_points; + gboolean from_llvm; guint8 *p; int generic_info_size; /* Load the method info from the AOT file */ p = ex_info; - code_len = decode_value (p, &p); flags = decode_value (p, &p); has_generic_jit_info = (flags & 1) != 0; has_dwarf_unwind_info = (flags & 2) != 0; has_clauses = (flags & 4) != 0; + has_seq_points = (flags & 8) != 0; + from_llvm = (flags & 16) != 0; if (has_dwarf_unwind_info) { guint32 offset; @@ -1438,12 +1960,23 @@ decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain, jinfo = mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + 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 (from_llvm) { + /* LLVM compiled method */ + /* The info is in the .eh_frame section */ +#ifdef TARGET_ARM + decode_arm_exidx (amodule, domain, method, code, code_len, jinfo); +#else + decode_eh_frame (amodule, domain, method, code, jinfo); +#endif + jinfo->from_llvm = 1; + } else { + 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; @@ -1463,6 +1996,37 @@ decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain, jinfo->method = decode_method_ref_2 (amodule, p, &p); } + if (has_seq_points) { + MonoSeqPointInfo *seq_points; + int il_offset, native_offset, last_il_offset, last_native_offset, j; + + int len = decode_value (p, &p); + + seq_points = g_malloc0 (sizeof (MonoSeqPointInfo) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (SeqPoint)); + seq_points->len = len; + last_il_offset = last_native_offset = 0; + for (i = 0; i < len; ++i) { + SeqPoint *sp = &seq_points->seq_points [i]; + il_offset = last_il_offset + decode_value (p, &p); + native_offset = last_native_offset + decode_value (p, &p); + + sp->il_offset = il_offset; + sp->native_offset = native_offset; + + sp->next_len = decode_value (p, &p); + sp->next = g_new (int, sp->next_len); + for (j = 0; j < sp->next_len; ++j) + sp->next [j] = decode_value (p, &p); + + last_il_offset = il_offset; + last_native_offset = native_offset; + } + + mono_domain_lock (domain); + g_hash_table_insert (domain_jit_info (domain)->seq_points, method, seq_points); + mono_domain_unlock (domain); + } + /* Load debug info */ buf_len = decode_value (p, &p); mono_debug_add_aot_method (domain, method, code, p, buf_len); @@ -1507,18 +2071,26 @@ mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len) return p; } +static int +compare_ints (const void *a, const void *b) +{ + return *(gint32*)a - *(gint32*)b; +} + MonoJitInfo * mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr) { - int pos, left, right, offset, offset1, offset2, last_offset, new_offset; - int page_index, method_index, table_len, is_wrapper; + int pos, left, right, offset, offset1, offset2, code_len; + int method_index, table_len, is_wrapper; guint32 token; MonoAotModule *amodule = image->aot_module; MonoMethod *method; MonoJitInfo *jinfo; guint8 *code, *ex_info, *p; - guint32 *table, *ptr; - gboolean found; + guint32 *table; + int nmethods = amodule->info.nmethods; + gint32 *code_offsets; + int i; if (!amodule) return NULL; @@ -1529,71 +2101,36 @@ mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr) offset = (guint8*)addr - amodule->code; - /* First search through the index */ - ptr = amodule->method_order; - last_offset = 0; - page_index = 0; - found = FALSE; - - if (*ptr == 0xffffff) - return NULL; - ptr ++; - - while (*ptr != 0xffffff) { - guint32 method_index = ptr [0]; - new_offset = amodule->code_offsets [method_index]; - - if (offset >= last_offset && offset < new_offset) { - found = TRUE; - break; + /* Compute a sorted table mapping code offsets to method indexes. */ + if (!amodule->sorted_code_offsets) { + code_offsets = g_new0 (gint32, nmethods * 2); + for (i = 0; i < nmethods; ++i) { + code_offsets [(i * 2)] = amodule->code_offsets [i]; + code_offsets [(i *2) + 1] = i; } + /* FIXME: Use a merge sort as this is mostly sorted */ + qsort (code_offsets, nmethods, sizeof (gint32) * 2, compare_ints); + for (i = 0; i < nmethods -1; ++i) + g_assert (code_offsets [(i * 2)] <= code_offsets [(i + 1) * 2]); - ptr ++; - last_offset = new_offset; - page_index ++; + if (InterlockedCompareExchangePointer ((gpointer*)&amodule->sorted_code_offsets, code_offsets, NULL) != NULL) + /* Somebody got in before us */ + g_free (code_offsets); } - /* Skip rest of index */ - while (*ptr != 0xffffff) - ptr ++; - ptr ++; + code_offsets = amodule->sorted_code_offsets; - table = ptr; - table_len = amodule->method_order_end - table; - - g_assert (table <= amodule->method_order_end); - - if (found) { - left = (page_index * 1024); - right = left + 1024; - - if (right > table_len) - right = table_len; - - offset1 = amodule->code_offsets [table [left]]; - g_assert (offset1 <= offset); - - //printf ("Found in index: 0x%x 0x%x 0x%x\n", offset, last_offset, new_offset); - } - else { - //printf ("Not found in index: 0x%x\n", offset); - left = 0; - right = table_len; - } - - /* Binary search inside the method_order table to find the method */ + /* Binary search in the sorted_code_offsets table */ + left = 0; + right = nmethods; while (TRUE) { pos = (left + right) / 2; - g_assert (table + pos <= amodule->method_order_end); - - //printf ("Pos: %5d < %5d < %5d Offset: 0x%05x < 0x%05x < 0x%05x\n", left, pos, right, amodule->code_offsets [table [left]], offset, amodule->code_offsets [table [right]]); - - offset1 = amodule->code_offsets [table [pos]]; - if (table + pos + 1 >= amodule->method_order_end) + offset1 = code_offsets [(pos * 2)]; + if (pos + 1 == nmethods) offset2 = amodule->code_end - amodule->code; else - offset2 = amodule->code_offsets [table [pos + 1]]; + offset2 = code_offsets [(pos + 1) * 2]; if (offset < offset1) right = pos; @@ -1603,7 +2140,20 @@ mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr) break; } - method_index = table [pos]; + g_assert (offset >= code_offsets [(pos * 2)]); + if (pos + 1 < nmethods) + g_assert (offset < code_offsets [((pos + 1) * 2)]); + method_index = code_offsets [(pos * 2) + 1]; + + code = &amodule->code [amodule->code_offsets [method_index]]; + ex_info = &amodule->blob [mono_aot_get_offset (amodule->ex_info_offsets, method_index)]; + + if (pos == nmethods - 1) + code_len = amodule->code_end - code; + else + code_len = code_offsets [(pos + 1) * 2] - code_offsets [pos * 2]; + + g_assert ((guint8*)code <= (guint8*)addr && (guint8*)addr < (guint8*)code + code_len); /* Might be a wrapper/extra method */ if (amodule->extra_methods) { @@ -1640,7 +2190,7 @@ mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr) break; } - p = amodule->extra_method_info + table [(pos * 2) + 1]; + p = amodule->blob + table [(pos * 2) + 1]; is_wrapper = decode_value (p, &p); g_assert (!is_wrapper); method = decode_method_ref_2 (amodule, p, &p); @@ -1655,11 +2205,8 @@ mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr) g_assert (method); //printf ("F: %s\n", mono_method_full_name (method, TRUE)); - - code = &amodule->code [amodule->code_offsets [method_index]]; - ex_info = &amodule->ex_info [amodule->ex_info_offsets [method_index]]; - - jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, code); + + jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, addr, code, code_len); g_assert ((guint8*)addr >= (guint8*)jinfo->code_start); g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size); @@ -1735,7 +2282,7 @@ decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guin goto cleanup; break; case MONO_PATCH_INFO_IMAGE: - ji->data.image = load_image (aot_module, decode_value (p, &p)); + ji->data.image = load_image (aot_module, decode_value (p, &p), TRUE); if (!ji->data.image) goto cleanup; break; @@ -1764,16 +2311,18 @@ decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guin } case MONO_PATCH_INFO_R8: { guint32 val [2]; + guint64 v; ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (double)); val [0] = decode_value (p, &p); val [1] = decode_value (p, &p); - *(double*)ji->data.target = *(double*)val; + v = ((guint64)val [1] << 32) | ((guint64)val [0]); + *(double*)ji->data.target = *(double*)&v; break; } case MONO_PATCH_INFO_LDSTR: - image = load_image (aot_module, decode_value (p, &p)); + image = load_image (aot_module, decode_value (p, &p), TRUE); if (!image) goto cleanup; ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p)); @@ -1783,7 +2332,7 @@ decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guin case MONO_PATCH_INFO_LDTOKEN: case MONO_PATCH_INFO_TYPE_FROM_HANDLE: /* Shared */ - image = load_image (aot_module, decode_value (p, &p)); + image = load_image (aot_module, decode_value (p, &p), TRUE); if (!image) goto cleanup; ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p)); @@ -1826,6 +2375,17 @@ decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guin ji->data.rgctx_entry = entry; break; } + case MONO_PATCH_INFO_SEQ_POINT_INFO: + break; + case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE: { + MonoJumpInfoImtTramp *imt_tramp = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoImtTramp)); + + imt_tramp->method = decode_method_ref_2 (aot_module, p, &p); + imt_tramp->vt_offset = decode_value (p, &p); + + ji->data.imt_tramp = imt_tramp; + break; + } default: g_warning ("unhandled type %d", ji->type); g_assert_not_reached (); @@ -1866,7 +2426,7 @@ load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches, /* Already loaded */ //printf ("HIT!\n"); } else { - shared_p = aot_module->got_info + aot_module->got_info_offsets [got_offset]; + shared_p = aot_module->blob + mono_aot_get_offset (aot_module->got_info_offsets, got_offset); ji->type = decode_value (shared_p, &shared_p); @@ -1924,14 +2484,14 @@ load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoM MonoMemPool *mp; int i, pindex, n_patches, used_strings; gboolean keep_patches = TRUE; - guint8 *p, *ex_info; + guint8 *p; MonoJitInfo *jinfo = NULL; guint8 *code, *info; if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE) return NULL; - if ((domain != mono_get_root_domain ()) && (!(amodule->opts & MONO_OPT_SHARED))) + if ((domain != mono_get_root_domain ()) && (!(amodule->info.opts & MONO_OPT_SHARED))) /* Non shared AOT code can't be used in other appdomains */ return NULL; @@ -1952,7 +2512,8 @@ load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoM } code = &amodule->code [amodule->code_offsets [method_index]]; - info = &amodule->method_info [amodule->method_info_offsets [method_index]]; + + info = &amodule->blob [mono_aot_get_offset (amodule->method_info_offsets, method_index)]; mono_aot_lock (); if (!amodule->methods_loaded) @@ -1966,10 +2527,15 @@ load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoM if (mono_jit_stats.methods_aot >= mono_last_aot_method) return NULL; else if (mono_jit_stats.methods_aot == mono_last_aot_method - 1) { - if (method) - printf ("LAST AOT METHOD: %s%s%s.%s.\n", method->klass->name_space, method->klass->name_space [0] ? "." : "", method->klass->name, method->name); - else + if (!method) + method = mono_get_method (image, token, NULL); + if (method) { + char *name = mono_method_full_name (method, TRUE); + printf ("LAST AOT METHOD: %s.\n", name); + g_free (name); + } else { printf ("LAST AOT METHOD: %p %d\n", code, method_index); + } } } @@ -1982,7 +2548,7 @@ load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoM klass = decode_klass_ref (amodule, p, &p); } - if (amodule->opts & MONO_OPT_SHARED) + if (amodule->info.opts & MONO_OPT_SHARED) used_strings = decode_value (p, &p); else used_strings = 0; @@ -1992,7 +2558,7 @@ load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoM mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (token)); } - if (amodule->opts & MONO_OPT_SHARED) + if (amodule->info.opts & MONO_OPT_SHARED) keep_patches = FALSE; n_patches = decode_value (p, &p); @@ -2039,10 +2605,8 @@ load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoM full_name = mono_method_full_name (method, TRUE); - if (!jinfo) { - ex_info = &amodule->ex_info [amodule->ex_info_offsets [method_index]]; - jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, code); - } + if (!jinfo) + jinfo = mono_aot_find_jit_info (domain, amodule->assembly->image, code); mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND AOT compiled code for %s %p - %p %p\n", full_name, code, code + jinfo->code_size, info); g_free (full_name); @@ -2061,6 +2625,19 @@ load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoM mono_aot_unlock (); + if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION) { + MonoJitInfo *jinfo; + + if (!method) { + method = mono_get_method (image, token, NULL); + g_assert (method); + } + mono_profiler_method_jit (method); + jinfo = mono_jit_info_table_find (domain, (char*)code); + g_assert (jinfo); + mono_profiler_method_end_jit (method, jinfo, MONO_PROFILE_OK); + } + if (from_plt && klass && !klass->generic_container) mono_runtime_class_init (mono_class_vtable (domain, klass)); @@ -2068,7 +2645,7 @@ load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoM cleanup: /* FIXME: The space in domain->mp is wasted */ - if (amodule->opts & MONO_OPT_SHARED) + if (amodule->info.opts & MONO_OPT_SHARED) /* No need to cache patches */ mono_mempool_destroy (mp); @@ -2079,11 +2656,10 @@ load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoM } static guint32 -find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method) +find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method, const char *name) { guint32 table_size, entry_size, hash; guint32 *table, *entry; - char *name = NULL; guint32 index; static guint32 n_extra_decodes; @@ -2094,10 +2670,6 @@ find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method) table = amodule->extra_method_table + 1; entry_size = 3; - if (method->wrapper_type) { - name = mono_aot_wrapper_name (method); - } - hash = mono_aot_method_hash (method) % table_size; entry = &table [hash * entry_size]; @@ -2114,7 +2686,7 @@ find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method) guint8 *p; int is_wrapper_name; - p = amodule->extra_method_info + key; + p = amodule->blob + key; is_wrapper_name = decode_value (p, &p); if (is_wrapper_name) { int wrapper_type = decode_value (p, &p); @@ -2168,7 +2740,6 @@ find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method) break; } - g_free (name); return index; } @@ -2191,12 +2762,18 @@ find_extra_method (MonoMethod *method, MonoAotModule **out_amodule) guint32 index; GPtrArray *modules; int i; + char *name = NULL; + + if (method->wrapper_type) + name = mono_aot_wrapper_name (method); /* Try the method's module first */ *out_amodule = method->klass->image->aot_module; - index = find_extra_method_in_amodule (method->klass->image->aot_module, method); - if (index != 0xffffff) + index = find_extra_method_in_amodule (method->klass->image->aot_module, method, name); + if (index != 0xffffff) { + g_free (name); return index; + } /* * Try all other modules. @@ -2216,7 +2793,7 @@ find_extra_method (MonoMethod *method, MonoAotModule **out_amodule) MonoAotModule *amodule = g_ptr_array_index (modules, i); if (amodule != method->klass->image->aot_module) - index = find_extra_method_in_amodule (amodule, method); + index = find_extra_method_in_amodule (amodule, method, name); if (index != 0xffffff) { *out_amodule = amodule; break; @@ -2225,6 +2802,7 @@ find_extra_method (MonoMethod *method, MonoAotModule **out_amodule) g_ptr_array_free (modules, TRUE); + g_free (name); return index; } @@ -2284,42 +2862,15 @@ mono_aot_get_method (MonoDomain *domain, MonoMethod *method) * method in Array. */ if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED && method->klass->rank && strstr (method->name, "System.Collections.Generic")) { - MonoMethod *m; - const char *prefix; - MonoGenericContext ctx; - MonoType *args [16]; - char *mname, *iname, *s, *s2, *helper_name = NULL; - - prefix = "System.Collections.Generic"; - s = g_strdup_printf ("%s", method->name + strlen (prefix) + 1); - s2 = strstr (s, "`1."); - g_assert (s2); - s2 [0] = '\0'; - iname = s; - mname = s2 + 3; - - //printf ("X: %s %s\n", iname, mname); - - if (!strcmp (iname, "IList")) - helper_name = g_strdup_printf ("InternalArray__%s", mname); - else - helper_name = g_strdup_printf ("InternalArray__%s_%s", iname, mname); - m = mono_class_get_method_from_name (mono_defaults.array_class, helper_name, mono_method_signature (method)->param_count); - g_assert (m); - g_free (helper_name); - g_free (s); - - if (m->is_generic) { - memset (&ctx, 0, sizeof (ctx)); - args [0] = &method->klass->element_class->byval_arg; - ctx.method_inst = mono_metadata_get_generic_inst (1, args); - m = mono_class_inflate_generic_method (m, &ctx); - } + MonoMethod *m = mono_aot_get_array_helper_from_wrapper (method); code = mono_aot_get_method (domain, m); if (code) { - if (mono_method_needs_static_rgctx_invoke (m, FALSE)) - code = mono_create_static_rgctx_trampoline (m, code); + if (mono_method_needs_static_rgctx_invoke (m, FALSE)) { + code = mono_create_static_rgctx_trampoline (m, mono_create_ftnptr (domain, code)); + /* The call above returns an ftnptr */ + code = mono_get_addr_from_ftnptr (code); + } return code; } @@ -2477,6 +3028,7 @@ find_aot_module (guint8 *code) * * This function is called by the entries in the PLT to resolve the actual method that * needs to be called. It returns a trampoline to the method and patches the PLT entry. + * Returns NULL if the something cannot be loaded. */ gpointer mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code) @@ -2485,19 +3037,22 @@ mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code guint8 *p, *target, *plt_entry; MonoJumpInfo ji; MonoAotModule *module = (MonoAotModule*)aot_module; - gboolean res; + gboolean res, no_ftnptr = FALSE; MonoMemPool *mp; //printf ("DYN: %p %d\n", aot_module, plt_info_offset); - p = &module->got_info [plt_info_offset]; + p = &module->blob [plt_info_offset]; ji.type = decode_value (p, &p); mp = mono_mempool_new_size (512); res = decode_patch (module, mp, &ji, p, &p); - // FIXME: Error handling (how ?) - g_assert (res); + + if (!res) { + mono_mempool_destroy (mp); + return NULL; + } /* * Avoid calling resolve_patch_target in the full-aot case if possible, since @@ -2508,10 +3063,32 @@ mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code if (mono_aot_only && ji.type == MONO_PATCH_INFO_METHOD && !ji.data.method->is_generic && !mono_method_check_context_used (ji.data.method) && !(ji.data.method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) && !mono_method_needs_static_rgctx_invoke (ji.data.method, FALSE)) { target = mono_jit_compile_method (ji.data.method); + no_ftnptr = TRUE; } else { target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE); } + /* + * The trampoline expects us to return a function descriptor on platforms which use + * it, but resolve_patch_target returns a direct function pointer for some type of + * patches, so have to translate between the two. + * FIXME: Clean this up, but how ? + */ + if (ji.type == MONO_PATCH_INFO_ABS || ji.type == MONO_PATCH_INFO_INTERNAL_METHOD || ji.type == MONO_PATCH_INFO_CLASS_INIT || ji.type == MONO_PATCH_INFO_ICALL_ADDR || ji.type == MONO_PATCH_INFO_JIT_ICALL_ADDR || ji.type == MONO_PATCH_INFO_RGCTX_FETCH) { + /* These should already have a function descriptor */ +#ifdef PPC_USES_FUNCTION_DESCRIPTOR + /* Our function descriptors have a 0 environment, gcc created ones don't */ + if (ji.type != MONO_PATCH_INFO_INTERNAL_METHOD && ji.type != MONO_PATCH_INFO_JIT_ICALL_ADDR && ji.type != MONO_PATCH_INFO_ICALL_ADDR) + g_assert (((gpointer*)target) [2] == 0); +#endif + /* Empty */ + } else if (!no_ftnptr) { +#ifdef PPC_USES_FUNCTION_DESCRIPTOR + g_assert (((gpointer*)target) [2] != 0); +#endif + target = mono_create_ftnptr (mono_domain_get (), target); + } + mono_mempool_destroy (mp); /* Patch the PLT entry with target which might be the actual method not a trampoline */ @@ -2712,6 +3289,8 @@ load_function (MonoAotModule *amodule, const char *name) /* Nothing to patch */ return code; + p = amodule->blob + *(guint32*)p; + /* Similar to mono_aot_load_method () */ n_patches = decode_value (p, &p); @@ -2964,7 +3543,8 @@ mono_aot_get_lazy_fetch_trampoline (guint32 slot) symbol = g_strdup_printf ("rgctx_fetch_trampoline_%u", slot); code = load_function (mono_defaults.corlib->aot_module, symbol); g_free (symbol); - return code; + /* The caller expects an ftnptr */ + return mono_create_ftnptr (mono_domain_get (), code); } gpointer @@ -2997,6 +3577,98 @@ mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem return code; } + +/* + * mono_aot_set_make_unreadable: + * + * Set whenever to make all mmaped memory unreadable. In conjuction with a + * SIGSEGV handler, this is useful to find out which pages the runtime tries to read. + */ +void +mono_aot_set_make_unreadable (gboolean unreadable) +{ + static int inited; + + make_unreadable = unreadable; + + if (make_unreadable && !inited) { + mono_counters_register ("AOT pagefaults", MONO_COUNTER_JIT | MONO_COUNTER_INT, &n_pagefaults); + } +} + +typedef struct { + MonoAotModule *module; + guint8 *ptr; +} FindMapUserData; + +static void +find_map (gpointer key, gpointer value, gpointer user_data) +{ + MonoAotModule *module = (MonoAotModule*)value; + FindMapUserData *data = (FindMapUserData*)user_data; + + if (!data->module) + if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end)) + data->module = module; +} + +static MonoAotModule* +find_module_for_addr (void *ptr) +{ + FindMapUserData data; + + if (!make_unreadable) + return NULL; + + data.module = NULL; + data.ptr = (guint8*)ptr; + + mono_aot_lock (); + g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data); + mono_aot_unlock (); + + return data.module; +} + +/* + * mono_aot_is_pagefault: + * + * Should be called from a SIGSEGV signal handler to find out whenever @ptr is + * within memory allocated by this module. + */ +gboolean +mono_aot_is_pagefault (void *ptr) +{ + if (!make_unreadable) + return FALSE; + + /* + * Not signal safe, but SIGSEGV's are synchronous, and + * this is only turned on by a MONO_DEBUG option. + */ + return find_module_for_addr (ptr) != NULL; +} + +/* + * mono_aot_handle_pagefault: + * + * Handle a pagefault caused by an unreadable page by making it readable again. + */ +void +mono_aot_handle_pagefault (void *ptr) +{ +#ifndef PLATFORM_WIN32 + guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), mono_pagesize ()); + int res; + + mono_aot_lock (); + res = mono_mprotect (start, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_EXEC); + g_assert (res == 0); + + n_pagefaults ++; + mono_aot_unlock (); +#endif +} #else /* AOT disabled */