Merge pull request #2819 from BrzVlad/fix-major-log
[mono.git] / mono / mini / image-writer.c
index 819cfa33b22da7bb8f0aaf6f258bea04acc31bd5..4992565ba0d162951515dee73e1177f56124f4f8 100644 (file)
 #define USE_ELF_RELA 1
 #endif
 
-#if defined(TARGET_X86) && !defined(TARGET_WIN32) && !defined(__APPLE__)
+#if defined(TARGET_X86) && !defined(HOST_WIN32) && !defined(__APPLE__)
 #define USE_ELF_WRITER 1
 #endif
 
 #if defined(TARGET_ARM) && !defined(TARGET_MACH) && !defined(HOST_WIN32)
-#define USE_ELF_WRITER 1
+//#define USE_ELF_WRITER 1
 #endif
 
 #if defined(__mips__)
@@ -324,7 +324,7 @@ bin_writer_emit_ensure_buffer (BinSection *section, int size)
                guint8 *data;
                while (new_size <= new_offset)
                        new_size *= 2;
-               data = g_malloc0 (new_size);
+               data = (guint8 *)g_malloc0 (new_size);
 #ifdef __native_client_codegen__
                /* for Native Client, fill empty space with HLT instruction */
                /* instead of 00.                                           */
@@ -448,7 +448,7 @@ static BinReloc*
 create_reloc (MonoImageWriter *acfg, const char *end, const char* start, int offset)
 {
        BinReloc *reloc;
-       reloc = mono_mempool_alloc0 (acfg->mempool, sizeof (BinReloc));
+       reloc = (BinReloc *)mono_mempool_alloc0 (acfg->mempool, sizeof (BinReloc));
        reloc->val1 = mono_mempool_strdup (acfg->mempool, end);
        if (strcmp (start, ".") == 0) {
                reloc->val2_section = acfg->cur_section;
@@ -899,7 +899,7 @@ get_label_addr (MonoImageWriter *acfg, const char *name)
        BinSection *section;
        gsize value;
 
-       lab = g_hash_table_lookup (acfg->labels, name);
+       lab = (BinLabel *)g_hash_table_lookup (acfg->labels, name);
        if (!lab)
                g_error ("Undefined label: '%s'.\n", name);
        section = lab->section;
@@ -982,7 +982,7 @@ collect_syms (MonoImageWriter *acfg, int *hash, ElfStrTable *strtab, ElfSectHead
                /*g_print ("sym name %s tabled to %d\n", symbol->name, symbols [i].st_name);*/
                section = symbol->section;
                symbols [i].st_shndx = section->parent? section->parent->shidx: section->shidx;
-               lab = g_hash_table_lookup (acfg->labels, symbol->name);
+               lab = (BinLabel *)g_hash_table_lookup (acfg->labels, symbol->name);
                offset = lab->offset;
                if (section->parent) {
                        symbols [i].st_value = section->parent->virt_offset + section->cur_offset + offset;
@@ -991,7 +991,7 @@ collect_syms (MonoImageWriter *acfg, int *hash, ElfStrTable *strtab, ElfSectHead
                }
 
                if (symbol->end_label) {
-                       BinLabel *elab = g_hash_table_lookup (acfg->labels, symbol->end_label);
+                       BinLabel *elab = (BinLabel *)g_hash_table_lookup (acfg->labels, symbol->end_label);
                        g_assert (elab);
                        symbols [i].st_size = elab->offset - lab->offset;
                }
@@ -1063,7 +1063,7 @@ reloc_symbols (MonoImageWriter *acfg, ElfSymbol *symbols, ElfSectHeader *sheader
                if (dynamic && !symbol->is_global)
                        continue;
                section = symbol->section;
-               lab = g_hash_table_lookup (acfg->labels, symbol->name);
+               lab = (BinLabel *)g_hash_table_lookup (acfg->labels, symbol->name);
                offset = lab->offset;
                if (section->parent) {
                        symbols [i].st_value = sheaders [section->parent->shidx].sh_addr + section->cur_offset + offset;
@@ -1259,7 +1259,6 @@ static int normal_sections [] = { SECT_DATA, SECT_DEBUG_FRAME, SECT_DEBUG_INFO,
 static int
 bin_writer_emit_writeout (MonoImageWriter *acfg)
 {
-       FILE *file;
        ElfHeader header;
        ElfProgHeader progh [4];
        ElfSectHeader secth [SECT_NUM];
@@ -1277,11 +1276,9 @@ bin_writer_emit_writeout (MonoImageWriter *acfg)
        ElfSymbol *symtab;
        ElfDynamic dynamic [14];
        int *hash;
-       int i, num_sections, file_offset, virt_offset, size, num_symtab;
+       int i, num_sections, file_offset, virt_offset, size;
        int num_local_syms;
 
-       file = acfg->fp;
-
        /* Section headers */
        memset (&secth, 0, sizeof (secth));
        memset (&dynamic, 0, sizeof (dynamic));
@@ -1305,7 +1302,6 @@ bin_writer_emit_writeout (MonoImageWriter *acfg)
 
        num_sections = collect_sections (acfg, secth, all_sections, 16);
        hash = build_hash (acfg, num_sections, &dyn_str_table);
-       num_symtab = hash [1]; /* FIXME */
 #if 0
        g_print ("num_sections: %d\n", num_sections);
        g_print ("dynsym: %d, dynstr size: %d\n", hash [1], (int)dyn_str_table.data->len);
@@ -1619,7 +1615,7 @@ bin_writer_emit_writeout (MonoImageWriter *acfg)
 
        if (!acfg->fp) {
                acfg->out_buf_size = file_offset + sizeof (secth);
-               acfg->out_buf = g_malloc (acfg->out_buf_size);
+               acfg->out_buf = (guint8 *)g_malloc (acfg->out_buf_size);
        }
 
        bin_writer_fwrite (acfg, &header, sizeof (header), 1);
@@ -1690,6 +1686,9 @@ bin_writer_emit_writeout (MonoImageWriter *acfg)
 static void
 asm_writer_emit_start (MonoImageWriter *acfg)
 {
+#if defined(TARGET_ASM_APPLE)
+       fprintf (acfg->fp, ".subsections_via_symbols\n");
+#endif
 }
 
 static int
@@ -1763,8 +1762,14 @@ asm_writer_emit_symbol_type (MonoImageWriter *acfg, const char *name, gboolean f
                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
@@ -1787,7 +1792,7 @@ asm_writer_emit_local_symbol (MonoImageWriter *acfg, const char *name, const cha
 {
        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
 
@@ -1799,7 +1804,8 @@ asm_writer_emit_symbol_size (MonoImageWriter *acfg, const char *name, const char
 {
        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
 }
@@ -2005,7 +2011,7 @@ asm_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
 /* EMIT FUNCTIONS */
 
 void
-img_writer_emit_start (MonoImageWriter *acfg)
+mono_img_writer_emit_start (MonoImageWriter *acfg)
 {
 #ifdef USE_BIN_WRITER
        if (acfg->use_bin_writer)
@@ -2018,7 +2024,7 @@ img_writer_emit_start (MonoImageWriter *acfg)
 }
 
 void
-img_writer_emit_section_change (MonoImageWriter *acfg, const char *section_name, int subsection_index)
+mono_img_writer_emit_section_change (MonoImageWriter *acfg, const char *section_name, int subsection_index)
 {
 #ifdef USE_BIN_WRITER
        if (acfg->use_bin_writer)
@@ -2034,26 +2040,26 @@ img_writer_emit_section_change (MonoImageWriter *acfg, const char *section_name,
 }
 
 void
-img_writer_emit_push_section (MonoImageWriter *acfg, const char *section_name, int subsection)
+mono_img_writer_emit_push_section (MonoImageWriter *acfg, const char *section_name, int subsection)
 {
        g_assert (acfg->stack_pos < 16 - 1);
        acfg->section_stack [acfg->stack_pos] = acfg->current_section;
        acfg->subsection_stack [acfg->stack_pos] = acfg->current_subsection;
        acfg->stack_pos ++;
 
-       img_writer_emit_section_change (acfg, section_name, subsection);
+       mono_img_writer_emit_section_change (acfg, section_name, subsection);
 }
 
 void
-img_writer_emit_pop_section (MonoImageWriter *acfg)
+mono_img_writer_emit_pop_section (MonoImageWriter *acfg)
 {
        g_assert (acfg->stack_pos > 0);
        acfg->stack_pos --;
-       img_writer_emit_section_change (acfg, acfg->section_stack [acfg->stack_pos], acfg->subsection_stack [acfg->stack_pos]);
+       mono_img_writer_emit_section_change (acfg, acfg->section_stack [acfg->stack_pos], acfg->subsection_stack [acfg->stack_pos]);
 }
 
 void
-img_writer_set_section_addr (MonoImageWriter *acfg, guint64 addr)
+mono_img_writer_set_section_addr (MonoImageWriter *acfg, guint64 addr)
 {
 #ifdef USE_BIN_WRITER
        if (!acfg->use_bin_writer)
@@ -2066,7 +2072,7 @@ img_writer_set_section_addr (MonoImageWriter *acfg, guint64 addr)
 }
 
 void
-img_writer_emit_global (MonoImageWriter *acfg, const char *name, gboolean func)
+mono_img_writer_emit_global (MonoImageWriter *acfg, const char *name, gboolean func)
 {
 #ifdef USE_BIN_WRITER
        if (acfg->use_bin_writer)
@@ -2079,7 +2085,7 @@ img_writer_emit_global (MonoImageWriter *acfg, const char *name, gboolean func)
 }
 
 void
-img_writer_emit_local_symbol (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean func)
+mono_img_writer_emit_local_symbol (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean func)
 {
 #ifdef USE_BIN_WRITER
        if (acfg->use_bin_writer)
@@ -2092,14 +2098,14 @@ img_writer_emit_local_symbol (MonoImageWriter *acfg, const char *name, const cha
 }
 
 void
-img_writer_emit_symbol_size (MonoImageWriter *acfg, const char *name, const char *end_label)
+mono_img_writer_emit_symbol_size (MonoImageWriter *acfg, const char *name, const char *end_label)
 {
        if (!acfg->use_bin_writer)
                asm_writer_emit_symbol_size (acfg, name, end_label);
 }
 
 void
-img_writer_emit_label (MonoImageWriter *acfg, const char *name)
+mono_img_writer_emit_label (MonoImageWriter *acfg, const char *name)
 {
 #ifdef USE_BIN_WRITER
        if (acfg->use_bin_writer)
@@ -2112,7 +2118,7 @@ img_writer_emit_label (MonoImageWriter *acfg, const char *name)
 }
 
 void
-img_writer_emit_bytes (MonoImageWriter *acfg, const guint8* buf, int size)
+mono_img_writer_emit_bytes (MonoImageWriter *acfg, const guint8* buf, int size)
 {
 #ifdef USE_BIN_WRITER
        if (acfg->use_bin_writer)
@@ -2125,7 +2131,7 @@ img_writer_emit_bytes (MonoImageWriter *acfg, const guint8* buf, int size)
 }
 
 void
-img_writer_emit_string (MonoImageWriter *acfg, const char *value)
+mono_img_writer_emit_string (MonoImageWriter *acfg, const char *value)
 {
 #ifdef USE_BIN_WRITER
        if (acfg->use_bin_writer)
@@ -2138,7 +2144,7 @@ img_writer_emit_string (MonoImageWriter *acfg, const char *value)
 }
 
 void
-img_writer_emit_line (MonoImageWriter *acfg)
+mono_img_writer_emit_line (MonoImageWriter *acfg)
 {
 #ifdef USE_BIN_WRITER
        if (acfg->use_bin_writer)
@@ -2151,7 +2157,7 @@ img_writer_emit_line (MonoImageWriter *acfg)
 }
 
 void
-img_writer_emit_alignment (MonoImageWriter *acfg, int size)
+mono_img_writer_emit_alignment (MonoImageWriter *acfg, int size)
 {
 #ifdef USE_BIN_WRITER
        if (acfg->use_bin_writer)
@@ -2164,7 +2170,7 @@ img_writer_emit_alignment (MonoImageWriter *acfg, int size)
 }
 
 void
-img_writer_emit_alignment_fill (MonoImageWriter *acfg, int size, int fill)
+mono_img_writer_emit_alignment_fill (MonoImageWriter *acfg, int size, int fill)
 {
 #ifdef USE_BIN_WRITER
        if (acfg->use_bin_writer)
@@ -2178,7 +2184,7 @@ img_writer_emit_alignment_fill (MonoImageWriter *acfg, int size, int fill)
 
 #ifdef __native_client_codegen__
 void
-img_writer_emit_nacl_call_alignment (MonoImageWriter *acfg) {
+mono_img_writer_emit_nacl_call_alignment (MonoImageWriter *acfg) {
 #ifdef USE_BIN_WRITER
        if (acfg->use_bin_writer)
                bin_writer_emit_nacl_call_alignment (acfg);
@@ -2191,7 +2197,7 @@ img_writer_emit_nacl_call_alignment (MonoImageWriter *acfg) {
 #endif  /* __native_client_codegen__ */
 
 void
-img_writer_emit_pointer_unaligned (MonoImageWriter *acfg, const char *target)
+mono_img_writer_emit_pointer_unaligned (MonoImageWriter *acfg, const char *target)
 {
 #ifdef USE_BIN_WRITER
        if (acfg->use_bin_writer)
@@ -2204,7 +2210,7 @@ img_writer_emit_pointer_unaligned (MonoImageWriter *acfg, const char *target)
 }
 
 void
-img_writer_emit_pointer (MonoImageWriter *acfg, const char *target)
+mono_img_writer_emit_pointer (MonoImageWriter *acfg, const char *target)
 {
 #ifdef USE_BIN_WRITER
        if (acfg->use_bin_writer)
@@ -2217,7 +2223,7 @@ img_writer_emit_pointer (MonoImageWriter *acfg, const char *target)
 }
 
 void
-img_writer_emit_int16 (MonoImageWriter *acfg, int value)
+mono_img_writer_emit_int16 (MonoImageWriter *acfg, int value)
 {
 #ifdef USE_BIN_WRITER
        if (acfg->use_bin_writer)
@@ -2230,7 +2236,7 @@ img_writer_emit_int16 (MonoImageWriter *acfg, int value)
 }
 
 void
-img_writer_emit_int32 (MonoImageWriter *acfg, int value)
+mono_img_writer_emit_int32 (MonoImageWriter *acfg, int value)
 {
 #ifdef USE_BIN_WRITER
        if (acfg->use_bin_writer)
@@ -2243,7 +2249,7 @@ img_writer_emit_int32 (MonoImageWriter *acfg, int value)
 }
 
 void
-img_writer_emit_symbol_diff (MonoImageWriter *acfg, const char *end, const char* start, int offset)
+mono_img_writer_emit_symbol_diff (MonoImageWriter *acfg, const char *end, const char* start, int offset)
 {
 #ifdef USE_BIN_WRITER
        if (acfg->use_bin_writer)
@@ -2256,7 +2262,7 @@ img_writer_emit_symbol_diff (MonoImageWriter *acfg, const char *end, const char*
 }
 
 void
-img_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
+mono_img_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
 {
 #ifdef USE_BIN_WRITER
        if (acfg->use_bin_writer)
@@ -2269,7 +2275,7 @@ img_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
 }
 
 int
-img_writer_emit_writeout (MonoImageWriter *acfg)
+mono_img_writer_emit_writeout (MonoImageWriter *acfg)
 {
 #ifdef USE_BIN_WRITER
        if (acfg->use_bin_writer)
@@ -2282,9 +2288,9 @@ img_writer_emit_writeout (MonoImageWriter *acfg)
 }
 
 void
-img_writer_emit_byte (MonoImageWriter *acfg, guint8 val)
+mono_img_writer_emit_byte (MonoImageWriter *acfg, guint8 val)
 {
-       img_writer_emit_bytes (acfg, &val, 1);
+       mono_img_writer_emit_bytes (acfg, &val, 1);
 }
 
 /* 
@@ -2292,7 +2298,7 @@ img_writer_emit_byte (MonoImageWriter *acfg, guint8 val)
  * Do not advance PC.
  */
 void
-img_writer_emit_reloc (MonoImageWriter *acfg, int reloc_type, const char *symbol, int addend)
+mono_img_writer_emit_reloc (MonoImageWriter *acfg, int reloc_type, const char *symbol, int addend)
 {
        /* This is only supported by the bin writer */
 #ifdef USE_BIN_WRITER
@@ -2306,26 +2312,26 @@ img_writer_emit_reloc (MonoImageWriter *acfg, int reloc_type, const char *symbol
 }
 
 /*
- * img_writer_emit_unset_mode:
+ * mono_img_writer_emit_unset_mode:
  *
  *   Flush buffered data so it is safe to write to the output file from outside this
  * module. This is a nop for the binary writer.
  */
 void
-img_writer_emit_unset_mode (MonoImageWriter *acfg)
+mono_img_writer_emit_unset_mode (MonoImageWriter *acfg)
 {
        if (!acfg->use_bin_writer)
                asm_writer_emit_unset_mode (acfg);
 }
 
 /*
- * img_writer_get_output:
+ * mono_img_writer_get_output:
  *
  *   Return the output buffer of a binary writer emitting to memory. The returned memory
  * is from malloc, and it is owned by the caller.
  */
 guint8*
-img_writer_get_output (MonoImageWriter *acfg, guint32 *size)
+mono_img_writer_get_output (MonoImageWriter *acfg, guint32 *size)
 {
 #ifdef USE_BIN_WRITER
        guint8 *buf;
@@ -2346,7 +2352,7 @@ img_writer_get_output (MonoImageWriter *acfg, guint32 *size)
  * Return whenever the binary writer is supported on this platform.
  */
 gboolean
-bin_writer_supported (void)
+mono_bin_writer_supported (void)
 {
 #ifdef USE_BIN_WRITER
        return TRUE;
@@ -2356,14 +2362,14 @@ bin_writer_supported (void)
 }
 
 /*
- * img_writer_create:
+ * mono_img_writer_create:
  *
  *   Create an image writer writing to FP. If USE_BIN_WRITER is TRUE, FP can be NULL,
  * in this case the image writer will write to a memory buffer obtainable by calling
- * img_writer_get_output ().
+ * mono_img_writer_get_output ().
  */
 MonoImageWriter*
-img_writer_create (FILE *fp, gboolean use_bin_writer)
+mono_img_writer_create (FILE *fp, gboolean use_bin_writer)
 {
        MonoImageWriter *w = g_new0 (MonoImageWriter, 1);
        
@@ -2382,7 +2388,7 @@ img_writer_create (FILE *fp, gboolean use_bin_writer)
 }
 
 void
-img_writer_destroy (MonoImageWriter *w)
+mono_img_writer_destroy (MonoImageWriter *w)
 {
        // FIXME: Free all the stuff
        mono_mempool_destroy (w->mempool);
@@ -2390,7 +2396,7 @@ img_writer_destroy (MonoImageWriter *w)
 }
 
 gboolean
-img_writer_subsections_supported (MonoImageWriter *acfg)
+mono_img_writer_subsections_supported (MonoImageWriter *acfg)
 {
 #ifdef TARGET_ASM_APPLE
        return acfg->use_bin_writer;
@@ -2400,13 +2406,13 @@ img_writer_subsections_supported (MonoImageWriter *acfg)
 }
 
 FILE *
-img_writer_get_fp (MonoImageWriter *acfg)
+mono_img_writer_get_fp (MonoImageWriter *acfg)
 {
        return acfg->fp;
 }
 
 const char *
-img_writer_get_temp_label_prefix (MonoImageWriter *acfg)
+mono_img_writer_get_temp_label_prefix (MonoImageWriter *acfg)
 {
        return AS_TEMP_LABEL_PREFIX;
 }