[aot] Rework the handling of preinitialized got slots, instead of hardcoding their offsets into the code, initialize them the same way the other got slots are initialized, by decoding a MonoJumpInfo structure for them, and resolving them using mono_resolve_patch_target ().

This commit is contained in:
Zoltan Varga committed 2015-10-14 01:45:23 -04:00
1 parent 948d79daba
commit 51c4e47f22
4 files changed
+129 -110

No files matched your search

+12 -10
View File
@@ -7291,6 +7291,12 @@ mono_aot_get_got_offset (MonoJumpInfo *ji)
return get_got_offset (llvm_acfg, TRUE, ji);
}
/*
* mono_aot_is_shared_got_offset:
*
* Return whenever OFFSET refers to a GOT slot which is preinitialized
* when the AOT image is loaded.
*/
gboolean
mono_aot_is_shared_got_offset (int offset)
{
@@ -8603,6 +8609,7 @@ init_aot_file_info (MonoAotCompile *acfg, MonoAotFileInfo *info, int gc_name_off
info->long_align = MONO_ABI_ALIGNOF (gint64);
info->generic_tramp_num = MONO_TRAMPOLINE_NUM;
info->tramp_page_size = acfg->tramp_page_size;
info->nshared_got_entries = acfg->nshared_got_entries;
for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
info->tramp_page_code_offsets [i] = acfg->tramp_page_code_offsets [i];
}
@@ -8718,6 +8725,7 @@ emit_aot_file_info (MonoAotCompile *acfg, MonoAotFileInfo *info)
emit_int32 (acfg, info->long_align);
emit_int32 (acfg, info->generic_tramp_num);
emit_int32 (acfg, info->tramp_page_size);
emit_int32 (acfg, info->nshared_got_entries);
for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
emit_int32 (acfg, info->num_trampolines [i]);
@@ -9633,68 +9641,62 @@ mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
}
#endif
/*
* Allocate the first few GOT entries to information which is needed frequently, or it is needed
* during method initialization etc.
*/
{
MonoJumpInfo *ji;
/* Slot 0 */
ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
ji->type = MONO_PATCH_INFO_IMAGE;
ji->data.image = acfg->image;
get_got_offset (acfg, FALSE, ji);
get_got_offset (acfg, TRUE, ji);
/* Slot 1 */
ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
ji->type = MONO_PATCH_INFO_MSCORLIB_GOT_ADDR;
get_got_offset (acfg, FALSE, ji);
get_got_offset (acfg, TRUE, ji);
/* Slot 2 */
ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
ji->type = MONO_PATCH_INFO_GC_CARD_TABLE_ADDR;
get_got_offset (acfg, FALSE, ji);
get_got_offset (acfg, TRUE, ji);
/* Slot 3 */
ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
ji->type = MONO_PATCH_INFO_GC_NURSERY_START;
get_got_offset (acfg, FALSE, ji);
get_got_offset (acfg, TRUE, ji);
/* Slot 4 */
ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
ji->type = MONO_PATCH_INFO_JIT_TLS_ID;
get_got_offset (acfg, FALSE, ji);
get_got_offset (acfg, TRUE, ji);
/* Slot 5 */
ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
ji->type = MONO_PATCH_INFO_AOT_MODULE;
get_got_offset (acfg, FALSE, ji);
get_got_offset (acfg, TRUE, ji);
/* Slot 6 */
ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoAotCompile));
ji->type = MONO_PATCH_INFO_INTERNAL_METHOD;
ji->data.name = "mono_aot_init_llvm_method";
get_got_offset (acfg, FALSE, ji);
get_got_offset (acfg, TRUE, ji);
/* Slot 7 */
ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoAotCompile));
ji->type = MONO_PATCH_INFO_INTERNAL_METHOD;
ji->data.name = "mono_aot_init_gshared_method_this";
get_got_offset (acfg, FALSE, ji);
get_got_offset (acfg, TRUE, ji);
/* Slot 8 */
ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoAotCompile));
ji->type = MONO_PATCH_INFO_INTERNAL_METHOD;
ji->data.name = "mono_aot_init_gshared_method_rgctx";
get_got_offset (acfg, FALSE, ji);
get_got_offset (acfg, TRUE, ji);
/* Slot 9 */
ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoAotCompile));
ji->type = MONO_PATCH_INFO_INTERNAL_METHOD;
ji->data.name = "mono_llvm_throw_corlib_exception";
+108 -96
View File
@@ -84,7 +84,7 @@ typedef struct MonoAotModule {
/* Pointer to the Global Offset Table */
gpointer *got;
gpointer *llvm_got;
gpointer shared_got [N_COMMON_GOT_ENTRIES];
gpointer *shared_got;
GHashTable *name_cache;
GHashTable *extra_methods;
/* Maps methods to their code */
@@ -215,6 +215,9 @@ compute_llvm_code_range (MonoAotModule *amodule, guint8 **code_start, guint8 **c
static gboolean
init_method (MonoAotModule *amodule, guint32 method_index, MonoMethod *method, MonoClass *init_class, MonoGenericContext *context);
static MonoJumpInfo*
decode_patches (MonoAotModule *amodule, MonoMemPool *mp, int n_patches, gboolean llvm, guint32 *got_offsets);
static inline void
amodule_lock (MonoAotModule *amodule)
{
@@ -1752,42 +1755,62 @@ get_call_table_entry (void *table, int index)
#endif
}
static void
init_gots (MonoAotModule *amodule)
{
int i;
if (amodule->got) {
for (i = 0; i < N_COMMON_GOT_ENTRIES; ++i)
amodule->got [i] = amodule->shared_got [i];
}
if (amodule->llvm_got) {
for (i = 0; i < N_COMMON_GOT_ENTRIES; ++i)
amodule->llvm_got [i] = amodule->shared_got [i];
}
}
/*
* init_amodule_got:
*
* Initialize the shared got entries for AMODULE.
*/
static void
init_amodule_got (MonoAotModule *amodule)
{
MonoJumpInfo ji;
MonoJumpInfo *ji;
MonoMemPool *mp = mono_mempool_new ();
MonoJumpInfo *patches;
guint32 got_offsets [128];
int i, npatches;
/* These can't be initialized in load_aot_module () */
if (!amodule->shared_got [6] && !amodule->got_initializing) {
amodule->got_initializing = TRUE;
if (amodule->shared_got [0] || amodule->got_initializing)
return;
memset (&ji, 0, sizeof (ji));
ji.type = MONO_PATCH_INFO_INTERNAL_METHOD;
ji.data.name = "mono_aot_init_llvm_method";
amodule->got_initializing = TRUE;
amodule->shared_got [6] = mono_resolve_patch_target (NULL, mono_get_root_domain (), NULL, &ji, FALSE);
ji.data.name = "mono_aot_init_gshared_method_this";
amodule->shared_got [7] = mono_resolve_patch_target (NULL, mono_get_root_domain (), NULL, &ji, FALSE);
ji.data.name = "mono_aot_init_gshared_method_rgctx";
amodule->shared_got [8] = mono_resolve_patch_target (NULL, mono_get_root_domain (), NULL, &ji, FALSE);
ji.data.name = "mono_llvm_throw_corlib_exception";
amodule->shared_got [9] = mono_resolve_patch_target (NULL, mono_get_root_domain (), NULL, &ji, FALSE);
init_gots (amodule);
amodule->got_initializing = TRUE;
npatches = amodule->info.nshared_got_entries;
for (i = 0; i < npatches; ++i)
got_offsets [i] = i;
patches = decode_patches (amodule, mp, npatches, FALSE, got_offsets);
g_assert (patches);
for (i = 0; i < npatches; ++i) {
ji = &patches [i];
if (ji->type == MONO_PATCH_INFO_GC_CARD_TABLE_ADDR && !mono_gc_is_moving ()) {
amodule->shared_got [i] = NULL;
} else if (ji->type == MONO_PATCH_INFO_GC_NURSERY_START && !mono_gc_is_moving ()) {
amodule->shared_got [i] = NULL;
} else if (ji->type == MONO_PATCH_INFO_IMAGE) {
amodule->shared_got [i] = amodule->assembly->image;
} else if (ji->type == MONO_PATCH_INFO_MSCORLIB_GOT_ADDR) {
if (mono_defaults.corlib) {
MonoAotModule *mscorlib_amodule = mono_defaults.corlib->aot_module;
amodule->shared_got [i] = mscorlib_amodule->got;
} else {
amodule->shared_got [i] = amodule->got;
}
} else if (ji->type == MONO_PATCH_INFO_AOT_MODULE) {
amodule->shared_got [i] = amodule;
} else {
amodule->shared_got [i] = mono_resolve_patch_target (NULL, mono_get_root_domain (), NULL, ji, FALSE);
}
}
if (amodule->got) {
for (i = 0; i < npatches; ++i)
amodule->got [i] = amodule->shared_got [i];
}
if (amodule->llvm_got) {
for (i = 0; i < npatches; ++i)
amodule->llvm_got [i] = amodule->shared_got [i];
}
}
@@ -1920,6 +1943,7 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
amodule->sofile = sofile;
amodule->method_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL);
amodule->blob = blob;
amodule->shared_got = g_new0 (gpointer, info->nshared_got_entries);
mono_mutex_init_recursive (&amodule->mutex);
@@ -2058,40 +2082,13 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
assembly->image->aot_module = amodule;
amodule->shared_got [0] = assembly->image;
if (mono_aot_only) {
char *code;
find_symbol (amodule->sofile, amodule->globals, "specific_trampolines_page", (gpointer *)&code);
amodule->use_page_trampolines = code != NULL;
/*g_warning ("using page trampolines: %d", amodule->use_page_trampolines);*/
if (mono_defaults.corlib) {
/* The second got slot contains the mscorlib got addr */
MonoAotModule *mscorlib_amodule = mono_defaults.corlib->aot_module;
amodule->shared_got [1] = mscorlib_amodule->got;
} else {
amodule->shared_got [1] = amodule->got;
}
}
if (mono_gc_is_moving ()) {
MonoJumpInfo ji;
memset (&ji, 0, sizeof (ji));
ji.type = MONO_PATCH_INFO_GC_CARD_TABLE_ADDR;
amodule->shared_got [2] = mono_resolve_patch_target (NULL, mono_get_root_domain (), NULL, &ji, FALSE);
memset (&ji, 0, sizeof (ji));
ji.type = MONO_PATCH_INFO_GC_NURSERY_START;
amodule->shared_got [3] = mono_resolve_patch_target (NULL, mono_get_root_domain (), NULL, &ji, FALSE);
}
amodule->shared_got [5] = amodule;
/* Can't initialize the other shared got slots yet, they are initialized in init_amodule_got () */
init_gots (amodule);
/*
* Register the plt region as a single trampoline so we can unwind from this code
*/
@@ -3444,6 +3441,7 @@ decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guin
}
case MONO_PATCH_INFO_SEQ_POINT_INFO:
case MONO_PATCH_INFO_AOT_MODULE:
case MONO_PATCH_INFO_MSCORLIB_GOT_ADDR:
break;
case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE: {
MonoJumpInfoImtTramp *imt_tramp = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoImtTramp));
@@ -3544,63 +3542,77 @@ decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guin
return FALSE;
}
/*
* decode_patches:
*
* Decode a list of patches identified by the got offsets in GOT_OFFSETS. Return an array of
* MonoJumpInfo structures allocated from MP.
*/
static MonoJumpInfo*
load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
decode_patches (MonoAotModule *amodule, MonoMemPool *mp, int n_patches, gboolean llvm, guint32 *got_offsets)
{
MonoJumpInfo *patches;
MonoJumpInfo *ji;
gpointer *got;
guint32 *got_info_offsets;
int i;
gboolean res;
if (llvm) {
got = amodule->llvm_got;
got_info_offsets = amodule->info.llvm_got_info_offsets;
} else {
got = amodule->got;
got_info_offsets = amodule->info.got_info_offsets;
}
patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
for (i = 0; i < n_patches; ++i) {
guint8 *p = amodule->blob + mono_aot_get_offset (got_info_offsets, got_offsets [i]);
ji = &patches [i];
ji->type = decode_value (p, &p);
/* See load_method () for SFLDA */
if (got [got_offsets [i]] && ji->type != MONO_PATCH_INFO_SFLDA) {
/* Already loaded */
} else {
res = decode_patch (amodule, mp, ji, p, &p);
if (!res)
return NULL;
}
}
return patches;
}
static MonoJumpInfo*
load_patch_info (MonoAotModule *amodule, MonoMemPool *mp, int n_patches,
gboolean llvm, guint32 **got_slots,
guint8 *buf, guint8 **endbuf)
{
MonoJumpInfo *patches;
int pindex;
guint8 *p;
gpointer *got;
guint32 *got_offsets;
p = buf;
if (llvm) {
got = aot_module->llvm_got;
got_offsets = aot_module->info.llvm_got_info_offsets;
} else {
got = aot_module->got;
got_offsets = aot_module->info.got_info_offsets;
}
patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
*got_slots = g_malloc (sizeof (guint32) * n_patches);
for (pindex = 0; pindex < n_patches; ++pindex) {
MonoJumpInfo *ji = &patches [pindex];
guint8 *shared_p;
gboolean res;
guint32 got_offset;
(*got_slots)[pindex] = decode_value (p, &p);
}
got_offset = decode_value (p, &p);
shared_p = aot_module->blob + mono_aot_get_offset (got_offsets, got_offset);
ji->type = decode_value (shared_p, &shared_p);
/* See load_method () for SFLDA */
if (got [got_offset] && ji->type != MONO_PATCH_INFO_SFLDA) {
/* Already loaded */
} else {
res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
if (!res)
goto cleanup;
}
(*got_slots) [pindex] = got_offset;
patches = decode_patches (amodule, mp, n_patches, llvm, *got_slots);
if (!patches) {
g_free (*got_slots);
*got_slots = NULL;
return NULL;
}
*endbuf = p;
return patches;
cleanup:
g_free (*got_slots);
*got_slots = NULL;
return NULL;
}
static void
+3 -2
View File
@@ -7088,7 +7088,7 @@ emit_aot_file_info (MonoLLVMModule *lmodule)
info = &lmodule->aot_info;
/* Create an LLVM type to represent MonoAotFileInfo */
nfields = 2 + MONO_AOT_FILE_INFO_NUM_SYMBOLS + 13 + 4;
nfields = 2 + MONO_AOT_FILE_INFO_NUM_SYMBOLS + 14 + 4;
eltypes = g_new (LLVMTypeRef, nfields);
tindex = 0;
eltypes [tindex ++] = LLVMInt32Type ();
@@ -7097,7 +7097,7 @@ emit_aot_file_info (MonoLLVMModule *lmodule)
for (i = 0; i < MONO_AOT_FILE_INFO_NUM_SYMBOLS; ++i)
eltypes [tindex ++] = LLVMPointerType (LLVMInt8Type (), 0);
/* Scalars */
for (i = 0; i < 13; ++i)
for (i = 0; i < 14; ++i)
eltypes [tindex ++] = LLVMInt32Type ();
/* Arrays */
for (i = 0; i < 4; ++i)
@@ -7211,6 +7211,7 @@ emit_aot_file_info (MonoLLVMModule *lmodule)
fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->long_align, FALSE);
fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->generic_tramp_num, FALSE);
fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->tramp_page_size, FALSE);
fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->nshared_got_entries, FALSE);
/* Arrays */
fields [tindex ++] = llvm_array_from_uints (LLVMInt32Type (), info->num_trampolines, MONO_AOT_TRAMP_NUM);
fields [tindex ++] = llvm_array_from_uints (LLVMInt32Type (), info->trampoline_got_offset_base, MONO_AOT_TRAMP_NUM);
+6 -2
View File
@@ -113,7 +113,7 @@
#endif
/* Version number of the AOT file format */
#define MONO_AOT_FILE_VERSION 122
#define MONO_AOT_FILE_VERSION 123
//TODO: This is x86/amd64 specific.
#define mono_simd_shuffle_mask(a,b,c,d) ((a) | ((b) << 2) | ((c) << 4) | ((d) << 6))
@@ -267,7 +267,11 @@ typedef struct MonoAotFileInfo
guint32 double_align, long_align, generic_tramp_num;
/* The page size used by trampoline pages */
guint32 tramp_page_size;
/*
* The number of GOT entries which need to be preinitialized when the
* module is loaded.
*/
guint32 nshared_got_entries;
/* Arrays */
/* Number of trampolines */
guint32 num_trampolines [MONO_AOT_TRAMP_NUM];