Merge pull request #2324 from akoeplinger/caching-locking
[mono.git] / mono / mini / image-writer.c
index 13c055bd1e1ba03d072608f561c395b7bfae19be..c216f3b25755f4231091b096a4f506990e4a8d2f 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);
@@ -1843,6 +1839,19 @@ asm_writer_emit_alignment (MonoImageWriter *acfg, int size)
 #endif
 }
 
+#ifndef USE_BIN_WRITER
+static void 
+asm_writer_emit_alignment_fill (MonoImageWriter *acfg, int size, int fill)
+{
+       asm_writer_emit_unset_mode (acfg);
+#if defined(TARGET_ASM_APPLE)
+       fprintf (acfg->fp, "\t.align %d, 0x%0x\n", ilog2 (size), fill);
+#else
+       asm_writer_emit_alignment (acfg, size);
+#endif
+}
+#endif
+
 #ifdef __native_client_codegen__
 static void
 asm_writer_emit_nacl_call_alignment (MonoImageWriter *acfg) {
@@ -1992,7 +2001,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)
@@ -2005,7 +2014,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)
@@ -2021,26 +2030,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)
@@ -2053,7 +2062,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)
@@ -2066,7 +2075,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)
@@ -2079,14 +2088,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)
@@ -2099,7 +2108,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)
@@ -2112,7 +2121,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)
@@ -2125,7 +2134,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)
@@ -2138,7 +2147,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)
@@ -2150,9 +2159,22 @@ img_writer_emit_alignment (MonoImageWriter *acfg, int size)
 #endif
 }
 
+void
+mono_img_writer_emit_alignment_fill (MonoImageWriter *acfg, int size, int fill)
+{
+#ifdef USE_BIN_WRITER
+       if (acfg->use_bin_writer)
+               bin_writer_emit_alignment (acfg, size);
+       else
+               asm_writer_emit_alignment (acfg, size);
+#else
+       asm_writer_emit_alignment_fill (acfg, size, fill);
+#endif
+}
+
 #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);
@@ -2165,7 +2187,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)
@@ -2178,7 +2200,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)
@@ -2191,7 +2213,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)
@@ -2204,7 +2226,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)
@@ -2217,7 +2239,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)
@@ -2230,7 +2252,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)
@@ -2243,7 +2265,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)
@@ -2256,9 +2278,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);
 }
 
 /* 
@@ -2266,7 +2288,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
@@ -2280,26 +2302,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;
@@ -2320,7 +2342,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;
@@ -2330,14 +2352,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);
        
@@ -2356,7 +2378,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);
@@ -2364,7 +2386,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;
@@ -2374,13 +2396,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;
 }