Merge pull request #2794 from xmcclure/win-aot-basic
authormonojenkins <jo.shields+jenkins@xamarin.com>
Tue, 29 Mar 2016 16:31:13 +0000 (17:31 +0100)
committermonojenkins <jo.shields+jenkins@xamarin.com>
Tue, 29 Mar 2016 16:31:13 +0000 (17:31 +0100)
Make AOT compilation on 64 bit windows produce executables

This PR contains three commits (see commit descriptions) which:

- Fix 64-bit debug windows builds in MSVC 2015
- Make it possible to use the tool-prefix aot option with mono/mini make fullaotcheck (necessary if you are in cygwin and wish to invoke `x86_64-w64-mingw32-gcc`)
- Change mini so that `--aot=full` successfully emits 64-bit windows executables.

Building 32 bit executables will generally fail because we do not follow cdecl. The 64 bit executables do not run properly when executed.

1  2 
mono/mini/aot-compiler.c
mono/mini/image-writer.c

diff --combined mono/mini/aot-compiler.c
index 63327ec449843186befd139b376e058bcd0e0301,2e4bd331a5616fc0823bd08f09215cad938a7e14..b74b1107fbfb328cc1cd69361a9c3d68449052cd
@@@ -4966,7 -4966,7 +4966,7 @@@ emit_and_reloc_code (MonoAotCompile *ac
        GPtrArray *patches;
        MonoJumpInfo *patch_info;
        MonoDebugSourceLocation **locs = NULL;
 -      gboolean skip;
 +      gboolean skip, prologue_end = FALSE;
  #ifdef MONO_ARCH_AOT_SUPPORTED
        gboolean direct_call, external_call;
        guint32 got_slot;
                if (locs && locs [i]) {
                        MonoDebugSourceLocation *loc = locs [i];
                        int findex;
 +                      const char *options;
  
                        findex = get_file_index (acfg, loc->source_file);
                        emit_unset_mode (acfg);
 -                      fprintf (acfg->fp, ".loc %d %d 0\n", findex, loc->row);
 +                      if (!prologue_end)
 +                              options = " prologue_end";
 +                      else
 +                              options = "";
 +                      prologue_end = TRUE;
 +                      fprintf (acfg->fp, ".loc %d %d 0%s\n", findex, loc->row, options);
                        mono_debug_symfile_free_location (loc);
                }
  
@@@ -6676,6 -6670,15 +6676,15 @@@ wrap_path (gchar * path
        return clean;
  }
  
+ // Duplicate a char range and add it to a ptrarray, but only if it is nonempty
+ static void
+ ptr_array_add_range_if_nonempty(GPtrArray *args, gchar const *start, gchar const *end)
+ {
+       ptrdiff_t len = end-start;
+       if (len > 0)
+               g_ptr_array_add (args, g_strndup (start, len));
+ }
  static GPtrArray *
  mono_aot_split_options (const char *aot_options)
  {
  
        next:
                aot_options++;
+       restart:
                // If the next character is end of string, then process the last option.
                if (*(aot_options) == '\0') {
                        end_of_string = TRUE;
                continue;
  
        new_opt:
-               g_ptr_array_add (args, g_strndup (opt_start, aot_options - opt_start));
+               ptr_array_add_range_if_nonempty (args, opt_start, aot_options);
                opt_start = ++aot_options;
                if (end_of_string)
                        break;
-               goto next;
+               goto restart; // Check for null and continue loop
        }
  
        return args;
@@@ -9550,7 -9554,6 +9560,6 @@@ compile_asm (MonoAotCompile *acfg
  #define AS_OPTIONS "--64"
  #elif defined(TARGET_POWERPC64)
  #define AS_OPTIONS "-a64 -mppc64"
- #define LD_OPTIONS "-m elf64ppc"
  #elif defined(sparc) && SIZEOF_VOID_P == 8
  #define AS_OPTIONS "-xarch=v9"
  #elif defined(TARGET_X86) && defined(TARGET_MACH) && !defined(__native_client_codegen__)
  #define AS_NAME "as"
  #endif
  
- #ifndef LD_OPTIONS
- #define LD_OPTIONS ""
- #endif
  #if defined(sparc)
- #define LD_NAME "ld -shared -G"
+ #define LD_NAME "ld"
+ #define LD_OPTIONS "-shared -G"
  #elif defined(__ppc__) && defined(TARGET_MACH)
- #define LD_NAME "gcc -dynamiclib"
+ #define LD_NAME "gcc"
+ #define LD_OPTIONS "-dynamiclib"
  #elif defined(TARGET_AMD64) && defined(TARGET_MACH)
- #define LD_NAME "clang --shared"
+ #define LD_NAME "clang"
+ #define LD_OPTIONS "--shared"
  #elif defined(TARGET_WIN32) && !defined(TARGET_ANDROID)
- #define LD_NAME "gcc -shared --dll"
+ #define LD_NAME "gcc"
+ #define LD_OPTIONS "-shared"
  #elif defined(TARGET_X86) && defined(TARGET_MACH) && !defined(__native_client_codegen__)
- #define LD_NAME "clang -m32 -dynamiclib"
+ #define LD_NAME "clang"
+ #define LD_OPTIONS "-m32 -dynamiclib"
  #elif defined(TARGET_ARM) && !defined(TARGET_ANDROID)
- #define LD_NAME "gcc --shared"
+ #define LD_NAME "gcc"
+ #define LD_OPTIONS "--shared"
+ #elif defined(TARGET_POWERPC64)
+ #define LD_OPTIONS "-m elf64ppc"
+ #endif
+ #ifndef LD_OPTIONS
+ #define LD_OPTIONS ""
  #endif
  
        if (acfg->aot_opts.asm_only) {
                ld_flags = g_strdup_printf ("%s %s", ld_flags, "-lstdc++");
  
  #ifdef LD_NAME
-       command = g_strdup_printf ("%s -o %s %s %s %s", LD_NAME,
+       command = g_strdup_printf ("%s%s %s -o %s %s %s %s", tool_prefix, LD_NAME, LD_OPTIONS,
                wrap_path (tmp_outfile_name), wrap_path (llvm_ofile),
                wrap_path (g_strdup_printf ("%s.o", acfg->tmpfname)), ld_flags);
  #else
+       // Default (linux)
        command = g_strdup_printf ("\"%sld\" %s -shared -o %s %s %s %s", tool_prefix, LD_OPTIONS,
                wrap_path (tmp_outfile_name), wrap_path (llvm_ofile),
                wrap_path (g_strdup_printf ("%s.o", acfg->tmpfname)), ld_flags);
@@@ -10335,11 -10347,13 +10353,11 @@@ mono_compile_assembly (MonoAssembly *as
                }
        }
  
 -      if (acfg->aot_opts.dwarf_debug && acfg->aot_opts.asm_only && acfg->aot_opts.gnu_asm) {
 +      if (acfg->aot_opts.dwarf_debug && acfg->aot_opts.gnu_asm) {
                /*
                 * CLANG supports GAS .file/.loc directives, so emit line number information this way
 -               * FIXME: CLANG only emits line number info for .loc directives followed by assembly, not
 -               * .byte directives.
                 */
 -              //acfg->gas_line_numbers = TRUE;
 +              acfg->gas_line_numbers = TRUE;
        }
  
        if ((!acfg->aot_opts.nodebug || acfg->aot_opts.dwarf_debug) && acfg->has_jitted_code) {
                        aot_printerrf (acfg, "The dwarf AOT option requires the --debug option.\n");
                        return 1;
                }
 -              acfg->dwarf = mono_dwarf_writer_create (acfg->w, NULL, 0, FALSE, !acfg->gas_line_numbers);
 +              acfg->dwarf = mono_dwarf_writer_create (acfg->w, NULL, 0, !acfg->gas_line_numbers);
        }
  
        if (acfg->w)
diff --combined mono/mini/image-writer.c
index c060fdc99a66028193f1d3efa63033a18d6a0eca,92fd95a88cca55c1416778806b20316a84820bbe..4992565ba0d162951515dee73e1177f56124f4f8
@@@ -1686,9 -1686,6 +1686,9 @@@ bin_writer_emit_writeout (MonoImageWrit
  static void
  asm_writer_emit_start (MonoImageWriter *acfg)
  {
 +#if defined(TARGET_ASM_APPLE)
 +      fprintf (acfg->fp, ".subsections_via_symbols\n");
 +#endif
  }
  
  static int
@@@ -1762,8 -1759,14 +1762,14 @@@ asm_writer_emit_symbol_type (MonoImageW
                stype = "object";
  
        asm_writer_emit_unset_mode (acfg);
  #if defined(TARGET_ASM_APPLE)
  
+ #elif defined(TARGET_WIN32)
+       if (func)
+               fprintf (acfg->fp, "\t.def %s; .scl 2; .type 32; .endef\n", name);
+       else
+               fprintf (acfg->fp, "\t.data\n");
  #elif defined(TARGET_ARM)
        fprintf (acfg->fp, "\t.type %s,#%s\n", name, stype);
  #else
@@@ -1786,7 -1789,7 +1792,7 @@@ asm_writer_emit_local_symbol (MonoImage
  {
        asm_writer_emit_unset_mode (acfg);
  
- #ifndef TARGET_ASM_APPLE
+ #if !defined(TARGET_ASM_APPLE) && !defined(TARGET_WIN32)
        fprintf (acfg->fp, "\t.local %s\n", name);
  #endif
  
@@@ -1798,7 -1801,8 +1804,8 @@@ asm_writer_emit_symbol_size (MonoImageW
  {
        asm_writer_emit_unset_mode (acfg);
  
- #ifndef TARGET_ASM_APPLE
+ #if !defined(TARGET_ASM_APPLE) && !defined(TARGET_WIN32)
        fprintf (acfg->fp, "\t.size %s,%s-%s\n", name, end_label, name);
  #endif
  }