[aot] Fix static linking support when using llvm.
authorZoltan Varga <vargaz@gmail.com>
Tue, 7 Jul 2015 02:01:31 +0000 (22:01 -0400)
committerZoltan Varga <vargaz@gmail.com>
Tue, 7 Jul 2015 02:01:31 +0000 (22:01 -0400)
mono/mini/aot-compiler.c
mono/mini/mini-llvm-loaded.c
mono/mini/mini-llvm.c
mono/mini/mini.h

index ba22d13027e233c4888627bd75bdaedcc258a9c3..12bbc33c4bcc8e7ca322ab077d2994a6c5d9177f 100644 (file)
@@ -517,9 +517,9 @@ emit_info_symbol (MonoAotCompile *acfg, const char *name)
        if (acfg->llvm) {
                emit_label (acfg, name);
                /* LLVM generated code references this */
-               sprintf (symbol, "%s%s", acfg->user_symbol_prefix, name);
+               sprintf (symbol, "%s%s%s", acfg->user_symbol_prefix, acfg->global_prefix, name);
                emit_label (acfg, symbol);
-               emit_global (acfg, symbol, FALSE);
+               emit_global_inner (acfg, symbol, FALSE);
        } else {
                emit_label (acfg, name);
        }
@@ -8458,7 +8458,7 @@ emit_globals (MonoAotCompile *acfg)
        emit_section_change (acfg, ".data", 0);
        /* This is not a global, since it is accessed by the init function */
        emit_alignment (acfg, 8);
-       emit_label (acfg, symbol);
+       emit_info_symbol (acfg, symbol);
 
        sprintf (symbol, "%sglobals_hash", acfg->temp_prefix);
        emit_pointer (acfg, symbol);
@@ -9558,7 +9558,7 @@ mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
 #ifdef ENABLE_LLVM
        if (acfg->llvm) {
                llvm_acfg = acfg;
-               mono_llvm_create_aot_module (acfg->global_prefix, TRUE);
+               mono_llvm_create_aot_module (acfg->image->assembly, acfg->global_prefix, TRUE, acfg->aot_opts.static_link);
        }
 #endif
 
index db1fbe76654b8696bc07d035d2ec899f33b84964..c93a83728120c36d2acf7129423f905f4cb52856 100644 (file)
@@ -14,7 +14,7 @@
 typedef void (*MonoLLVMVoidFunc)(void);
 typedef void (*MonoLLVMCFGFunc)(MonoCompile *cfg);
 typedef void (*MonoLLVMEmitCallFunc)(MonoCompile *cfg, MonoCallInst *call);
-typedef void (*MonoLLVMCreateAotFunc)(const char *global_prefix, gboolean emit_dwarf);
+typedef void (*MonoLLVMCreateAotFunc)(MonoAssembly *assembly, const char *global_prefix, gboolean emit_dwarf, gboolean static_link);
 typedef void (*MonoLLVMEmitAotFunc)(const char *filename, const char *cu_name);
 typedef void (*MonoLLVMEmitAotInfoFunc)(MonoAotFileInfo *info, gboolean has_jitted_code);
 typedef void (*MonoLLVMEmitAotDataFunc)(const char *symbol, guint8 *data, int data_len);
@@ -56,10 +56,10 @@ mono_llvm_emit_call (MonoCompile *cfg, MonoCallInst *call)
 }
 
 void
-mono_llvm_create_aot_module (const char *global_prefix, gboolean emit_dwarf)
+mono_llvm_create_aot_module (MonoAssembly *assembly, const char *global_prefix, gboolean emit_dwarf, gboolean static_link)
 {
        g_assert (mono_llvm_create_aot_module_fptr);
-       mono_llvm_create_aot_module_fptr (global_prefix, emit_dwarf);
+       mono_llvm_create_aot_module_fptr (assembly, global_prefix, emit_dwarf, static_link);
 }
 
 void
index 0275fac1c4b5fe52b28383974a89e1bc9c644ac3..7cdcb84bc9838a39eaae3813b1f4349b773e895f 100644 (file)
@@ -65,11 +65,14 @@ typedef struct {
        int max_got_offset;
 
        /* For AOT */
+       MonoAssembly *assembly;
+       char *global_prefix;
        MonoAotFileInfo aot_info;
        const char *jit_got_symbol;
        const char *eh_frame_symbol;
        LLVMValueRef code_start, code_end;
        gboolean has_jitted_code;
+       gboolean static_link;
 } MonoLLVMModule;
 
 /*
@@ -5872,7 +5875,7 @@ mono_llvm_free_domain_info (MonoDomain *domain)
 }
 
 void
-mono_llvm_create_aot_module (const char *global_prefix, gboolean emit_dwarf)
+mono_llvm_create_aot_module (MonoAssembly *assembly, const char *global_prefix, gboolean emit_dwarf, gboolean static_link)
 {
        /* Delete previous module */
        if (aot_module.plt_entries)
@@ -5883,10 +5886,13 @@ mono_llvm_create_aot_module (const char *global_prefix, gboolean emit_dwarf)
        memset (&aot_module, 0, sizeof (aot_module));
 
        aot_module.module = LLVMModuleCreateWithName ("aot");
+       aot_module.assembly = assembly;
+       aot_module.global_prefix = g_strdup (global_prefix);
        aot_module.got_symbol = g_strdup_printf ("%s_llvm_got", global_prefix);
        aot_module.eh_frame_symbol = g_strdup_printf ("%s_eh_frame", global_prefix);
        aot_module.external_symbols = TRUE;
        aot_module.emit_dwarf = emit_dwarf;
+       aot_module.static_link = static_link;
        /* The first few entries are reserved */
        aot_module.max_got_offset = 16;
 
@@ -5970,10 +5976,25 @@ mono_llvm_emit_aot_data (const char *symbol, guint8 *data, int data_len)
 
        type = LLVMArrayType (LLVMInt8Type (), data_len);
        d = LLVMAddGlobal (lmodule->module, type, symbol);
+       LLVMSetVisibility (d, LLVMHiddenVisibility);
+       LLVMSetLinkage (d, LLVMInternalLinkage);
        LLVMSetInitializer (d, mono_llvm_create_constant_data_array (data, data_len));
        mono_llvm_set_is_constant (d);
 }
 
+/* Add a reference to a global defined in JITted code */
+static LLVMValueRef
+AddJitGlobal (MonoLLVMModule *lmodule, LLVMTypeRef type, const char *name)
+{
+       char *s;
+       LLVMValueRef v;
+
+       s = g_strdup_printf ("%s%s", lmodule->global_prefix, name);
+       v = LLVMAddGlobal (lmodule->module, type, s);
+       g_free (s);
+       return v;
+}
+
 static void
 emit_aot_file_info (MonoLLVMModule *lmodule)
 {
@@ -6006,6 +6027,10 @@ emit_aot_file_info (MonoLLVMModule *lmodule)
        LLVMStructSetBody (file_info_type, eltypes, nfields, FALSE);
 
        info_var = LLVMAddGlobal (aot_module.module, file_info_type, "mono_aot_file_info");
+       if (lmodule->static_link) {
+               LLVMSetVisibility (info_var, LLVMHiddenVisibility);
+               LLVMSetLinkage (info_var, LLVMInternalLinkage);
+       }
        fields = g_new (LLVMValueRef, nfields);
        tindex = 0;
        fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->version, FALSE);
@@ -6017,14 +6042,14 @@ emit_aot_file_info (MonoLLVMModule *lmodule)
         * for symbols defined in the .s file emitted by the aot compiler.
         */
        eltype = eltypes [tindex];
-       fields [tindex ++] = LLVMAddGlobal (aot_module.module, eltype, "jit_got");
-       fields [tindex ++] = aot_module.got_var;
+       fields [tindex ++] = AddJitGlobal (lmodule, eltype, "jit_got");
+       fields [tindex ++] = lmodule->got_var;
        /* llc defines this directly */
-       fields [tindex ++] = LLVMAddGlobal (aot_module.module, eltype, aot_module.eh_frame_symbol);
-       if (TRUE || aot_module.has_jitted_code) {
-               fields [tindex ++] = LLVMAddGlobal (aot_module.module, eltype, "jit_code_start");
-               fields [tindex ++] = LLVMAddGlobal (aot_module.module, eltype, "jit_code_end");
-               fields [tindex ++] = LLVMAddGlobal (aot_module.module, eltype, "method_addresses");
+       fields [tindex ++] = LLVMAddGlobal (lmodule->module, eltype, lmodule->eh_frame_symbol);
+       if (TRUE || lmodule->has_jitted_code) {
+               fields [tindex ++] = AddJitGlobal (lmodule, eltype, "jit_code_start");
+               fields [tindex ++] = AddJitGlobal (lmodule, eltype, "jit_code_end");
+               fields [tindex ++] = AddJitGlobal (lmodule, eltype, "method_addresses");
        } else {
                fields [tindex ++] = LLVMConstNull (eltype);
                fields [tindex ++] = LLVMConstNull (eltype);
@@ -6045,26 +6070,28 @@ emit_aot_file_info (MonoLLVMModule *lmodule)
        fields [tindex ++] = LLVMGetNamedGlobal (aot_module.module, "assembly_guid");
        fields [tindex ++] = LLVMGetNamedGlobal (aot_module.module, "runtime_version");
        if (info->trampoline_size [0]) {
-               fields [tindex ++] = LLVMAddGlobal (aot_module.module, eltype, "specific_trampolines");
-               fields [tindex ++] = LLVMAddGlobal (aot_module.module, eltype, "static_rgctx_trampolines");
-               fields [tindex ++] = LLVMAddGlobal (aot_module.module, eltype, "imt_thunks");
-               fields [tindex ++] = LLVMAddGlobal (aot_module.module, eltype, "gsharedvt_arg_trampolines");
+               fields [tindex ++] = AddJitGlobal (lmodule, eltype, "specific_trampolines");
+               fields [tindex ++] = AddJitGlobal (lmodule, eltype, "static_rgctx_trampolines");
+               fields [tindex ++] = AddJitGlobal (lmodule, eltype, "imt_thunks");
+               fields [tindex ++] = AddJitGlobal (lmodule, eltype, "gsharedvt_arg_trampolines");
        } else {
                fields [tindex ++] = LLVMConstNull (eltype);
                fields [tindex ++] = LLVMConstNull (eltype);
                fields [tindex ++] = LLVMConstNull (eltype);
                fields [tindex ++] = LLVMConstNull (eltype);
        }
-       // FIXME:
-       fields [tindex ++] = LLVMConstNull (eltype);
+       if (lmodule->static_link)
+               fields [tindex ++] = AddJitGlobal (lmodule, eltype, "globals");
+       else
+               fields [tindex ++] = LLVMConstNull (eltype);
        fields [tindex ++] = LLVMGetNamedGlobal (aot_module.module, "assembly_name");
        if (TRUE || aot_module.has_jitted_code) {
-               fields [tindex ++] = LLVMAddGlobal (aot_module.module, eltype, "plt");
-               fields [tindex ++] = LLVMAddGlobal (aot_module.module, eltype, "plt_end");
-               fields [tindex ++] = LLVMAddGlobal (aot_module.module, eltype, "unwind_info");
-               fields [tindex ++] = LLVMAddGlobal (aot_module.module, eltype, "unbox_trampolines");
-               fields [tindex ++] = LLVMAddGlobal (aot_module.module, eltype, "unbox_trampolines_end");
-               fields [tindex ++] = LLVMAddGlobal (aot_module.module, eltype, "unbox_trampoline_addresses");
+               fields [tindex ++] = AddJitGlobal (lmodule, eltype, "plt");
+               fields [tindex ++] = AddJitGlobal (lmodule, eltype, "plt_end");
+               fields [tindex ++] = AddJitGlobal (lmodule, eltype, "unwind_info");
+               fields [tindex ++] = AddJitGlobal (lmodule, eltype, "unbox_trampolines");
+               fields [tindex ++] = AddJitGlobal (lmodule, eltype, "unbox_trampolines_end");
+               fields [tindex ++] = AddJitGlobal (lmodule, eltype, "unbox_trampoline_addresses");
        } else {
                fields [tindex ++] = LLVMConstNull (eltype);
                fields [tindex ++] = LLVMConstNull (eltype);
@@ -6096,6 +6123,23 @@ emit_aot_file_info (MonoLLVMModule *lmodule)
        g_assert (tindex == nfields);
 
        LLVMSetInitializer (info_var, LLVMConstNamedStruct (file_info_type, fields, nfields));
+
+       if (lmodule->static_link) {
+               char *s, *p;
+               LLVMValueRef var;
+
+               s = g_strdup_printf ("mono_aot_module_%s_info", lmodule->assembly->aname.name);
+               /* Get rid of characters which cannot occur in symbols */
+               p = s;
+               for (p = s; *p; ++p) {
+                       if (!(isalnum (*p) || *p == '_'))
+                               *p = '_';
+               }
+               var = LLVMAddGlobal (lmodule->module, LLVMPointerType (LLVMInt8Type (), 0), s);
+               g_free (s);
+               LLVMSetInitializer (var, LLVMConstBitCast (LLVMGetNamedGlobal (aot_module.module, "mono_aot_file_info"), LLVMPointerType (LLVMInt8Type (), 0)));
+               LLVMSetLinkage (var, LLVMExternalLinkage);
+       }
 }
 
 /*
index 375e8c79f60d9f6be343f755a4698475aa894717..388628f02b9a0ddca1de8a74c8ee05f6548e15f4 100644 (file)
@@ -2334,12 +2334,12 @@ void     mono_save_trampoline_xdebug_info   (MonoTrampInfo *info);
 void     mono_xdebug_flush                  (void);
 
 /* LLVM backend */
-/* Keep this in synch with mini-llvm-loaded.c */
+/* KEEP THIS IN SYNCH WITH mini-llvm-loaded.c */
 void     mono_llvm_init                     (void) MONO_LLVM_INTERNAL;
 void     mono_llvm_cleanup                  (void) MONO_LLVM_INTERNAL;
 void     mono_llvm_emit_method              (MonoCompile *cfg) MONO_LLVM_INTERNAL;
 void     mono_llvm_emit_call                (MonoCompile *cfg, MonoCallInst *call) MONO_LLVM_INTERNAL;
-void     mono_llvm_create_aot_module        (const char *global_prefix, gboolean emit_dwarf) MONO_LLVM_INTERNAL;
+void     mono_llvm_create_aot_module        (MonoAssembly *assembly, const char *global_prefix, gboolean emit_dwarf, gboolean static_link) MONO_LLVM_INTERNAL;
 void     mono_llvm_emit_aot_module          (const char *filename, const char *cu_name) MONO_LLVM_INTERNAL;
 void     mono_llvm_emit_aot_file_info       (MonoAotFileInfo *info, gboolean has_jitted_code) MONO_LLVM_INTERNAL;
 void     mono_llvm_emit_aot_data            (const char *symbol, guint8 *data, int data_len) MONO_LLVM_INTERNAL;