71a57e88565bd7cd99822c05678f9744ad3d1063
[mono.git] / mono / mini / image-writer.c
1 /*
2  * image-writer.c: Creation of object files or assembly files using the same interface.
3  *
4  * Author:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *   Zoltan Varga (vargaz@gmail.com)
7  *   Paolo Molaro (lupus@ximian.com)
8  *
9  * (C) 2002 Ximian, Inc.
10  */
11
12 #include "config.h"
13 #include <sys/types.h>
14 #ifdef HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #ifdef HAVE_STDINT_H
18 #include <stdint.h>
19 #endif
20 #include <fcntl.h>
21 #include <ctype.h>
22 #include <string.h>
23 #ifndef PLATFORM_WIN32
24 #include <sys/time.h>
25 #else
26 #include <winsock2.h>
27 #include <windows.h>
28 #endif
29
30 #include <errno.h>
31 #include <sys/stat.h>
32 #include <limits.h>    /* for PAGESIZE */
33 #ifndef PAGESIZE
34 #define PAGESIZE 4096
35 #endif
36
37 #include "image-writer.h"
38
39 #ifndef PLATFORM_WIN32
40 #include <mono/utils/freebsd-elf32.h>
41 #include <mono/utils/freebsd-elf64.h>
42 #endif
43
44 #include "mini.h"
45
46 #define TV_DECLARE(name) gint64 name
47 #define TV_GETTIME(tv) tv = mono_100ns_ticks ()
48 #define TV_ELAPSED(start,end) (((end) - (start)) / 10)
49
50 /* 
51  * The used assembler dialect
52  * TARGET_ASM_APPLE == apple assembler on OSX
53  * TARGET_ASM_GAS == GNU assembler
54  */
55 #if !defined(TARGET_ASM_APPLE) && !defined(TARGET_ASM_GAS)
56 #ifdef __MACH__
57 #define TARGET_ASM_APPLE
58 #else
59 #define TARGET_ASM_GAS
60 #endif
61 #endif
62
63 /*
64  * Defines for the directives used by different assemblers
65  */
66 #if defined(__ppc__) || defined(__powerpc__) || defined(__MACH__)
67 #define AS_STRING_DIRECTIVE ".asciz"
68 #else
69 #define AS_STRING_DIRECTIVE ".string"
70 #endif
71
72 #define AS_INT32_DIRECTIVE ".long"
73 #define AS_INT64_DIRECTIVE ".quad"
74
75 #if defined(TARGET_AMD64) || defined(TARGET_POWERPC64)
76 #define AS_POINTER_DIRECTIVE ".quad"
77 #else
78 #define AS_POINTER_DIRECTIVE ".long"
79 #endif
80
81 #if defined(TARGET_ASM_APPLE)
82 #define AS_INT16_DIRECTIVE ".short"
83 #elif defined(TARGET_ASM_GAS)
84 #define AS_INT16_DIRECTIVE ".hword"
85 #else
86 #define AS_INT16_DIRECTIVE ".word"
87 #endif
88
89 #if defined(TARGET_ASM_APPLE)
90 #define AS_SKIP_DIRECTIVE ".space"
91 #else
92 #define AS_SKIP_DIRECTIVE ".skip"
93 #endif
94
95
96 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
97 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
98 #define ROUND_DOWN(VALUE,SIZE)  ((VALUE) & ~((SIZE) - 1))
99
100 #if defined(TARGET_AMD64) && !defined(PLATFORM_WIN32)
101 #define USE_ELF_WRITER 1
102 #define USE_ELF_RELA 1
103 #endif
104
105 #if defined(TARGET_X86) && !defined(PLATFORM_WIN32)
106 #define USE_ELF_WRITER 1
107 #endif
108
109 #if defined(TARGET_ARM) && !defined(__MACH__)
110 #define USE_ELF_WRITER 1
111 #endif
112
113 #if defined(__mips__)
114 #define USE_ELF_WRITER 1
115 #endif
116
117 #if defined(USE_ELF_WRITER)
118 #define USE_BIN_WRITER 1
119 #endif
120
121 #ifdef USE_BIN_WRITER
122
123 typedef struct _BinSymbol BinSymbol;
124 typedef struct _BinReloc BinReloc;
125 typedef struct _BinSection BinSection;
126
127 #endif
128
129 /* emit mode */
130 enum {
131         EMIT_NONE,
132         EMIT_BYTE,
133         EMIT_WORD,
134         EMIT_LONG
135 };
136
137 struct _MonoImageWriter {
138         MonoMemPool *mempool;
139         char *outfile;
140         gboolean use_bin_writer;
141         const char *current_section;
142         int current_subsection;
143         const char *section_stack [16];
144         int subsection_stack [16];
145         int stack_pos;
146         FILE *fp;
147         /* Bin writer */
148 #ifdef USE_BIN_WRITER
149         BinSymbol *symbols;
150         BinSection *sections;
151         BinSection *cur_section;
152         BinReloc *relocations;
153         GHashTable *labels;
154         int num_relocs;
155 #endif
156         /* Asm writer */
157         char *tmpfname;
158         int mode; /* emit mode */
159         int col_count; /* bytes emitted per .byte line */
160 };
161
162 static G_GNUC_UNUSED int
163 ilog2(register int value)
164 {
165         int count = -1;
166         while (value & ~0xf) count += 4, value >>= 4;
167         while (value) count++, value >>= 1;
168         return count;
169 }
170
171 #ifdef USE_BIN_WRITER
172
173 typedef struct _BinLabel BinLabel;
174 struct _BinLabel {
175         char *name;
176         BinSection *section;
177         int offset;
178 };
179
180 struct _BinReloc {
181         BinReloc *next;
182         char *val1;
183         char *val2;
184         BinSection *val2_section;
185         int val2_offset;
186         int offset;
187         BinSection *section;
188         int section_offset;
189         int reloc_type;
190 };
191
192 struct _BinSymbol {
193         BinSymbol *next;
194         char *name;
195         BinSection *section;
196         int offset;
197         gboolean is_function;
198         gboolean is_global;
199         char *end_label;
200 };
201
202 struct _BinSection {
203         BinSection *next;
204         BinSection *parent;
205         char *name;
206         int subsection;
207         guint8 *data;
208         int data_len;
209         int cur_offset;
210         int file_offset;
211         int virt_offset;
212         int shidx;
213 };
214
215 static void
216 bin_writer_emit_start (MonoImageWriter *acfg)
217 {
218         acfg->labels = g_hash_table_new (g_str_hash, g_str_equal);
219 }
220
221 static void
222 bin_writer_emit_section_change (MonoImageWriter *acfg, const char *section_name, int subsection_index)
223 {
224         BinSection *section;
225
226         if (acfg->cur_section && acfg->cur_section->subsection == subsection_index
227                         && strcmp (acfg->cur_section->name, section_name) == 0)
228                 return;
229         for (section = acfg->sections; section; section = section->next) {
230                 if (section->subsection == subsection_index && strcmp (section->name, section_name) == 0) {
231                         acfg->cur_section = section;
232                         return;
233                 }
234         }
235         if (!section) {
236                 section = g_new0 (BinSection, 1);
237                 section->name = g_strdup (section_name);
238                 section->subsection = subsection_index;
239                 section->next = acfg->sections;
240                 acfg->sections = section;
241                 acfg->cur_section = section;
242         }
243 }
244
245 static void
246 bin_writer_emit_symbol_inner (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean is_global, gboolean func)
247 {
248         BinSymbol *symbol = g_new0 (BinSymbol, 1);
249         symbol->name = g_strdup (name);
250         if (end_label)
251                 symbol->end_label = g_strdup (end_label);
252         symbol->is_function = func;
253         symbol->is_global = is_global;
254         symbol->section = acfg->cur_section;
255         /* FIXME: we align after this call... */
256         symbol->offset = symbol->section->cur_offset;
257         symbol->next = acfg->symbols;
258         acfg->symbols = symbol;
259 }
260
261 static void
262 bin_writer_emit_global (MonoImageWriter *acfg, const char *name, gboolean func)
263 {
264         bin_writer_emit_symbol_inner (acfg, name, NULL, TRUE, func);
265 }
266
267 static void
268 bin_writer_emit_local_symbol (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean func)
269 {
270         bin_writer_emit_symbol_inner (acfg, name, end_label, FALSE, func);
271 }
272
273 static void
274 bin_writer_emit_label (MonoImageWriter *acfg, const char *name)
275 {
276         BinLabel *label = g_new0 (BinLabel, 1);
277         label->name = g_strdup (name);
278         label->section = acfg->cur_section;
279         label->offset = acfg->cur_section->cur_offset;
280         g_hash_table_insert (acfg->labels, label->name, label);
281 }
282
283 static void
284 bin_writer_emit_ensure_buffer (BinSection *section, int size)
285 {
286         int new_offset = section->cur_offset + size;
287         if (new_offset >= section->data_len) {
288                 int new_size = section->data_len? section->data_len * 2: 256;
289                 guint8 *data;
290                 while (new_size <= new_offset)
291                         new_size *= 2;
292                 data = g_malloc0 (new_size);
293                 memcpy (data, section->data, section->data_len);
294                 g_free (section->data);
295                 section->data = data;
296                 section->data_len = new_size;
297         }
298 }
299
300 static void
301 bin_writer_emit_bytes (MonoImageWriter *acfg, const guint8* buf, int size)
302 {
303         bin_writer_emit_ensure_buffer (acfg->cur_section, size);
304         memcpy (acfg->cur_section->data + acfg->cur_section->cur_offset, buf, size);
305         acfg->cur_section->cur_offset += size;
306 }
307
308 static void
309 bin_writer_emit_string (MonoImageWriter *acfg, const char *value)
310 {
311         int size = strlen (value) + 1;
312         bin_writer_emit_bytes (acfg, (const guint8*)value, size);
313 }
314
315 static void
316 bin_writer_emit_line (MonoImageWriter *acfg)
317 {
318         /* Nothing to do in binary writer */
319 }
320
321 static void 
322 bin_writer_emit_alignment (MonoImageWriter *acfg, int size)
323 {
324         int offset = acfg->cur_section->cur_offset;
325         int add;
326         offset += (size - 1);
327         offset &= ~(size - 1);
328         add = offset - acfg->cur_section->cur_offset;
329         if (add) {
330                 bin_writer_emit_ensure_buffer (acfg->cur_section, add);
331                 acfg->cur_section->cur_offset += add;
332         }
333 }
334
335 static void
336 bin_writer_emit_pointer_unaligned (MonoImageWriter *acfg, const char *target)
337 {
338         BinReloc *reloc;
339
340         if (!target)
341                 // FIXME:
342                 g_assert_not_reached ();
343         reloc = g_new0 (BinReloc, 1);
344         reloc->val1 = g_strdup (target);
345         reloc->section = acfg->cur_section;
346         reloc->section_offset = acfg->cur_section->cur_offset;
347         reloc->next = acfg->relocations;
348         acfg->relocations = reloc;
349         if (strcmp (reloc->section->name, ".data") == 0) {
350                 acfg->num_relocs++;
351                 //g_print ("reloc: %s at %d\n", target, acfg->cur_section->cur_offset);
352         }
353         acfg->cur_section->cur_offset += sizeof (gpointer);
354 }
355
356 static void
357 bin_writer_emit_pointer (MonoImageWriter *acfg, const char *target)
358 {
359         bin_writer_emit_alignment (acfg, sizeof (gpointer));
360         bin_writer_emit_pointer_unaligned (acfg, target);
361 }
362
363 static void
364 bin_writer_emit_int16 (MonoImageWriter *acfg, int value)
365 {
366         guint8 *data;
367         bin_writer_emit_ensure_buffer (acfg->cur_section, 2);
368         data = acfg->cur_section->data + acfg->cur_section->cur_offset;
369         acfg->cur_section->cur_offset += 2;
370         /* FIXME: little endian */
371         data [0] = value;
372         data [1] = value >> 8;
373 }
374
375 static void
376 bin_writer_emit_int32 (MonoImageWriter *acfg, int value)
377 {
378         guint8 *data;
379         bin_writer_emit_ensure_buffer (acfg->cur_section, 4);
380         data = acfg->cur_section->data + acfg->cur_section->cur_offset;
381         acfg->cur_section->cur_offset += 4;
382         /* FIXME: little endian */
383         data [0] = value;
384         data [1] = value >> 8;
385         data [2] = value >> 16;
386         data [3] = value >> 24;
387 }
388
389 static BinReloc*
390 create_reloc (MonoImageWriter *acfg, const char *end, const char* start, int offset)
391 {
392         BinReloc *reloc;
393         reloc = mono_mempool_alloc0 (acfg->mempool, sizeof (BinReloc));
394         reloc->val1 = mono_mempool_strdup (acfg->mempool, end);
395         if (strcmp (start, ".") == 0) {
396                 reloc->val2_section = acfg->cur_section;
397                 reloc->val2_offset = acfg->cur_section->cur_offset;
398         } else {
399                 reloc->val2 = mono_mempool_strdup (acfg->mempool, start);
400         }
401         reloc->offset = offset;
402         reloc->section = acfg->cur_section;
403         reloc->section_offset = acfg->cur_section->cur_offset;
404         reloc->next = acfg->relocations;
405         acfg->relocations = reloc;
406         return reloc;
407 }
408
409 static void
410 bin_writer_emit_symbol_diff (MonoImageWriter *acfg, const char *end, const char* start, int offset)
411 {
412         create_reloc (acfg, end, start, offset);
413         acfg->cur_section->cur_offset += 4;
414         /*if (strcmp (reloc->section->name, ".data") == 0) {
415                 acfg->num_relocs++;
416                 g_print ("reloc: %s - %s + %d at %d\n", end, start, offset, acfg->cur_section->cur_offset - 4);
417         }*/
418 }
419
420 /* 
421  * Emit a relocation entry of type RELOC_TYPE against symbol SYMBOL at the current PC.
422  * Do not advance PC.
423  */
424 static G_GNUC_UNUSED void
425 bin_writer_emit_reloc (MonoImageWriter *acfg, int reloc_type, const char *symbol, int addend)
426 {
427         BinReloc *reloc = create_reloc (acfg, symbol, ".", addend);
428         reloc->reloc_type = reloc_type;
429 }
430
431 static void
432 bin_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
433 {
434         bin_writer_emit_ensure_buffer (acfg->cur_section, num);
435         acfg->cur_section->cur_offset += num;
436 }
437
438 #ifdef USE_ELF_WRITER
439
440 enum {
441         SECT_NULL,
442         SECT_HASH,
443         SECT_DYNSYM,
444         SECT_DYNSTR,
445         SECT_REL_DYN,
446         SECT_RELA_DYN,
447         SECT_TEXT,
448         SECT_DYNAMIC,
449         SECT_GOT_PLT,
450         SECT_DATA,
451         SECT_BSS,
452         SECT_DEBUG_FRAME,
453         SECT_DEBUG_INFO,
454         SECT_DEBUG_ABBREV,
455         SECT_DEBUG_LINE,
456         SECT_DEBUG_LOC,
457         SECT_SHSTRTAB,
458         SECT_SYMTAB,
459         SECT_STRTAB,
460         SECT_NUM
461 };
462
463 #if SIZEOF_VOID_P == 4
464
465 typedef Elf32_Ehdr ElfHeader;
466 typedef Elf32_Shdr ElfSectHeader;
467 typedef Elf32_Phdr ElfProgHeader;
468 typedef Elf32_Sym ElfSymbol;
469 typedef Elf32_Rel ElfReloc;
470 typedef Elf32_Rela ElfRelocA;
471 typedef Elf32_Dyn ElfDynamic;
472
473 #else
474
475 typedef Elf64_Ehdr ElfHeader;
476 typedef Elf64_Shdr ElfSectHeader;
477 typedef Elf64_Phdr ElfProgHeader;
478 typedef Elf64_Sym ElfSymbol;
479 typedef Elf64_Rel ElfReloc;
480 typedef Elf64_Rela ElfRelocA;
481 typedef Elf64_Dyn ElfDynamic;
482
483 #endif
484
485 typedef struct {
486         const char *name;
487         int type;
488         int esize;
489         int flags;
490         int align;
491 } SectInfo;
492
493 static SectInfo section_info [] = {
494         {"", 0, 0, 0, 0},
495         {".hash", SHT_HASH, 4, 2, SIZEOF_VOID_P},
496         {".dynsym", SHT_DYNSYM, sizeof (ElfSymbol), 2, SIZEOF_VOID_P},
497         {".dynstr", SHT_STRTAB, 0, 2, 1},
498         {".rel.dyn", SHT_REL, sizeof (ElfReloc), 2, SIZEOF_VOID_P},
499         {".rela.dyn", SHT_RELA, sizeof (ElfRelocA), 2, SIZEOF_VOID_P},
500         {".text", SHT_PROGBITS, 0, 6, 4096},
501         {".dynamic", SHT_DYNAMIC, sizeof (ElfDynamic), 3, SIZEOF_VOID_P},
502         {".got.plt", SHT_PROGBITS, SIZEOF_VOID_P, 3, SIZEOF_VOID_P},
503         {".data", SHT_PROGBITS, 0, 3, 8},
504         {".bss", SHT_NOBITS, 0, 3, 8},
505         {".debug_frame", SHT_PROGBITS, 0, 0, 8},
506         {".debug_info", SHT_PROGBITS, 0, 0, 1},
507         {".debug_abbrev", SHT_PROGBITS, 0, 0, 1},
508         {".debug_line", SHT_PROGBITS, 0, 0, 1},
509         {".debug_loc", SHT_PROGBITS, 0, 0, 1},
510         {".shstrtab", SHT_STRTAB, 0, 0, 1},
511         {".symtab", SHT_SYMTAB, sizeof (ElfSymbol), 0, SIZEOF_VOID_P},
512         {".strtab", SHT_STRTAB, 0, 0, 1}
513 };
514
515 typedef struct {
516         GString *data;
517         GHashTable *hash;
518 } ElfStrTable;
519
520 static int
521 str_table_add (ElfStrTable *table, const char* value)
522 {
523         int idx;
524         if (!table->data) {
525                 table->data = g_string_new_len ("", 1);
526                 table->hash = g_hash_table_new (g_str_hash, g_str_equal);
527         }
528         idx = GPOINTER_TO_UINT (g_hash_table_lookup (table->hash, value));
529         if (idx)
530                 return idx;
531         idx = table->data->len;
532         g_string_append (table->data, value);
533         g_string_append_c (table->data, 0);
534         g_hash_table_insert (table->hash, (void*)value, GUINT_TO_POINTER (idx));
535         return idx;
536 }
537
538 static void
539 append_subsection (MonoImageWriter *acfg, ElfSectHeader *sheaders, BinSection *sect, BinSection *add)
540 {
541         int offset = sect->cur_offset;
542         /*offset += (sheaders [sect->shidx].sh_addralign - 1);
543         offset &= ~(sheaders [sect->shidx].sh_addralign - 1);*/
544         /* 
545          * FIXME: we shouldn't align subsections at all, but if we don't then the
546          * stuff inside the subsections which is aligned won't get aligned.
547          */
548         if (strcmp (sect->name, ".debug_line") != 0) {
549                 offset += (8 - 1);
550                 offset &= ~(8 - 1);
551         }
552         bin_writer_emit_ensure_buffer (sect, offset);
553         //g_print ("section %s aligned to %d from %d\n", sect->name, offset, sect->cur_offset);
554         sect->cur_offset = offset;
555
556         bin_writer_emit_ensure_buffer (sect, add->cur_offset);
557         memcpy (sect->data + sect->cur_offset, add->data, add->cur_offset);
558         add->parent = sect;
559         sect->cur_offset += add->cur_offset;
560         add->cur_offset = offset; /* it becomes the offset in the parent section */
561         //g_print ("subsection %d of %s added at offset %d (align: %d)\n", add->subsection, sect->name, add->cur_offset, (int)sheaders [sect->shidx].sh_addralign);
562         add->data = NULL;
563         add->data_len = 0;
564 }
565
566 /* merge the subsections */
567 static int
568 collect_sections (MonoImageWriter *acfg, ElfSectHeader *sheaders, BinSection **out, int num)
569 {
570         int i, j, maxs, num_sections;
571         BinSection *sect;
572
573         num_sections = 0;
574         maxs = 0;
575         for (sect = acfg->sections; sect; sect = sect->next) {
576                 if (sect->subsection == 0) {
577                         out [num_sections++] = sect;
578                         g_assert (num_sections < num);
579                 }
580                 maxs = MAX (maxs, sect->subsection);
581         }
582         for (i = 0; i < num_sections; i++) {
583                 for (j = 1; j <= maxs; ++j) {
584                         for (sect = acfg->sections; sect; sect = sect->next) {
585                                 if (sect->subsection == j && strcmp (out [i]->name, sect->name) == 0) {
586                                         append_subsection (acfg, sheaders, out [i], sect);
587                                 }
588                         }
589                 }
590         }
591         return num_sections;
592 }
593
594 static unsigned long
595 elf_hash (const unsigned char *name)
596 {
597         unsigned long h = 0, g;
598         while (*name) {
599                 h = (h << 4) + *name++;
600                 if ((g = h & 0xf0000000))
601                         h ^= g >> 24;
602                 h &= ~g;
603         }
604         return h;
605 }
606
607 #define NUM_BUCKETS 17
608
609 static int*
610 build_hash (MonoImageWriter *acfg, int num_sections, ElfStrTable *dynstr)
611 {
612         int *data;
613         int num_symbols = 1 + num_sections + 3;
614         BinSymbol *symbol;
615
616         for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
617                 if (!symbol->is_global)
618                         continue;
619                 num_symbols++;
620                 str_table_add (dynstr, symbol->name);
621                 /*g_print ("adding sym: %s\n", symbol->name);*/
622         }
623         str_table_add (dynstr, "__bss_start");
624         str_table_add (dynstr, "_edata");
625         str_table_add (dynstr, "_end");
626
627         data = g_new0 (int, num_symbols + 2 + NUM_BUCKETS);
628         data [0] = NUM_BUCKETS;
629         data [1] = num_symbols;
630
631         return data;
632 }
633
634 static gsize
635 get_label_addr (MonoImageWriter *acfg, const char *name)
636 {
637         int offset;
638         BinLabel *lab;
639         BinSection *section;
640         gsize value;
641
642         lab = g_hash_table_lookup (acfg->labels, name);
643         if (!lab)
644                 g_error ("Undefined label: '%s'.\n", name);
645         section = lab->section;
646         offset = lab->offset;
647         if (section->parent) {
648                 value = section->parent->virt_offset + section->cur_offset + offset;
649         } else {
650                 value = section->virt_offset + offset;
651         }
652         return value;
653 }
654
655 static ElfSymbol*
656 collect_syms (MonoImageWriter *acfg, int *hash, ElfStrTable *strtab, ElfSectHeader *sheaders, int *num_syms)
657 {
658         ElfSymbol *symbols;
659         BinSymbol *symbol;
660         BinSection *section;
661         int i;
662         int *bucket;
663         int *chain;
664         unsigned long hashc;
665
666         if (hash)
667                 symbols = g_new0 (ElfSymbol, hash [1]);
668         else {
669                 i = 0;
670                 for (symbol = acfg->symbols; symbol; symbol = symbol->next)
671                         i ++;
672                 
673                 symbols = g_new0 (ElfSymbol, i + SECT_NUM + 10); /* FIXME */
674         }
675
676         /* the first symbol is undef, all zeroes */
677         i = 1;
678         if (sheaders) {
679                 int j;
680                 for (j = 1; j < SECT_NUM; ++j) {
681                         symbols [i].st_info = ELF32_ST_INFO (STB_LOCAL, STT_SECTION);
682                         symbols [i].st_shndx = j;
683                         symbols [i].st_value = sheaders [j].sh_addr;
684                         ++i;
685                 }
686         } else {
687                 for (section = acfg->sections; section; section = section->next) {
688                         if (section->parent)
689                                 continue;
690                         symbols [i].st_info = ELF32_ST_INFO (STB_LOCAL, STT_SECTION);
691                         if (strcmp (section->name, ".text") == 0) {
692                                 symbols [i].st_shndx = SECT_TEXT;
693                                 section->shidx = SECT_TEXT;
694                                 section->file_offset = 4096;
695                                 symbols [i].st_value = section->virt_offset;
696                         } else if (strcmp (section->name, ".data") == 0) {
697                                 symbols [i].st_shndx = SECT_DATA;
698                                 section->shidx = SECT_DATA;
699                                 section->file_offset = 4096 + 28; /* FIXME */
700                                 symbols [i].st_value = section->virt_offset;
701                         } else if (strcmp (section->name, ".bss") == 0) {
702                                 symbols [i].st_shndx = SECT_BSS;
703                                 section->shidx = SECT_BSS;
704                                 section->file_offset = 4096 + 28 + 8; /* FIXME */
705                                 symbols [i].st_value = section->virt_offset;
706                         }
707                         ++i;
708                 }
709         }
710         for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
711                 int offset;
712                 BinLabel *lab;
713                 if (!symbol->is_global && hash)
714                         continue;
715                 symbols [i].st_info = ELF32_ST_INFO (symbol->is_global ? STB_GLOBAL : STB_LOCAL, symbol->is_function? STT_FUNC : STT_OBJECT);
716                 symbols [i].st_name = str_table_add (strtab, symbol->name);
717                 /*g_print ("sym name %s tabled to %d\n", symbol->name, symbols [i].st_name);*/
718                 section = symbol->section;
719                 symbols [i].st_shndx = section->parent? section->parent->shidx: section->shidx;
720                 lab = g_hash_table_lookup (acfg->labels, symbol->name);
721                 offset = lab->offset;
722                 if (section->parent) {
723                         symbols [i].st_value = section->parent->virt_offset + section->cur_offset + offset;
724                 } else {
725                         symbols [i].st_value = section->virt_offset + offset;
726                 }
727
728                 if (symbol->end_label) {
729                         BinLabel *elab = g_hash_table_lookup (acfg->labels, symbol->end_label);
730                         g_assert (elab);
731                         symbols [i].st_size = elab->offset - lab->offset;
732                 }
733                 ++i;
734         }
735         /* add special symbols */
736         symbols [i].st_name = str_table_add (strtab, "__bss_start");
737         symbols [i].st_shndx = 0xfff1;
738         symbols [i].st_info = ELF32_ST_INFO (STB_GLOBAL, 0);
739         ++i;
740         symbols [i].st_name = str_table_add (strtab, "_edata");
741         symbols [i].st_shndx = 0xfff1;
742         symbols [i].st_info = ELF32_ST_INFO (STB_GLOBAL, 0);
743         ++i;
744         symbols [i].st_name = str_table_add (strtab, "_end");
745         symbols [i].st_shndx = 0xfff1;
746         symbols [i].st_info = ELF32_ST_INFO (STB_GLOBAL, 0);
747         ++i;
748
749         if (num_syms)
750                 *num_syms = i;
751
752         /* add to hash table */
753         if (hash) {
754                 bucket = hash + 2;
755                 chain = hash + 2 + hash [0];
756                 for (i = 0; i < hash [1]; ++i) {
757                         int slot;
758                         /*g_print ("checking %d '%s' (sym %d)\n", symbols [i].st_name, strtab->data->str + symbols [i].st_name, i);*/
759                         if (!symbols [i].st_name)
760                                 continue;
761                         hashc = elf_hash ((guint8*)strtab->data->str + symbols [i].st_name);
762                         slot = hashc % hash [0];
763                         /*g_print ("hashing '%s' at slot %d (sym %d)\n", strtab->data->str + symbols [i].st_name, slot, i);*/
764                         if (bucket [slot]) {
765                                 chain [i] = bucket [slot];
766                                 bucket [slot] = i;
767                         } else {
768                                 bucket [slot] = i;
769                         }
770                 }
771         }
772         return symbols;
773 }
774
775 static void
776 reloc_symbols (MonoImageWriter *acfg, ElfSymbol *symbols, ElfSectHeader *sheaders, ElfStrTable *strtab, gboolean dynamic)
777 {
778         BinSection *section;
779         BinSymbol *symbol;
780         int i;
781
782         i = 1;
783         if (dynamic) {
784                 for (section = acfg->sections; section; section = section->next) {
785                         if (section->parent)
786                                 continue;
787                         symbols [i].st_value = sheaders [section->shidx].sh_addr;
788                         ++i;
789                 }
790         } else {
791                 for (i = 1; i < SECT_NUM; ++i) {
792                         symbols [i].st_value = sheaders [i].sh_addr;
793                 }
794         }
795         for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
796                 int offset;
797                 BinLabel *lab;
798                 if (dynamic && !symbol->is_global)
799                         continue;
800                 section = symbol->section;
801                 lab = g_hash_table_lookup (acfg->labels, symbol->name);
802                 offset = lab->offset;
803                 if (section->parent) {
804                         symbols [i].st_value = sheaders [section->parent->shidx].sh_addr + section->cur_offset + offset;
805                 } else {
806                         symbols [i].st_value = sheaders [section->shidx].sh_addr + offset;
807                 }
808                 ++i;
809         }
810         /* __bss_start */
811         symbols [i].st_value = sheaders [SECT_BSS].sh_addr;
812         ++i;
813         /* _edata */
814         symbols [i].st_value = sheaders [SECT_DATA].sh_addr + sheaders [SECT_DATA].sh_size;
815         ++i;
816         /* _end */
817         symbols [i].st_value = sheaders [SECT_BSS].sh_addr + sheaders [SECT_BSS].sh_size;
818         ++i;
819 }
820
821 static void
822 resolve_reloc (MonoImageWriter *acfg, BinReloc *reloc, guint8 **out_data, gsize *out_vaddr, gsize *out_start_val, gsize *out_end_val)
823 {
824         guint8 *data;
825         gssize end_val, start_val;
826         gsize vaddr;
827
828         end_val = get_label_addr (acfg, reloc->val1);
829         if (reloc->val2) {
830                 start_val = get_label_addr (acfg, reloc->val2);
831         } else if (reloc->val2_section) {
832                 start_val = reloc->val2_offset;
833                 if (reloc->val2_section->parent)
834                         start_val += reloc->val2_section->parent->virt_offset + reloc->val2_section->cur_offset;
835                 else
836                         start_val += reloc->val2_section->virt_offset;
837         } else {
838                 start_val = 0;
839         }
840         end_val = end_val - start_val + reloc->offset;
841         if (reloc->section->parent) {
842                 data = reloc->section->parent->data;
843                 data += reloc->section->cur_offset;
844                 data += reloc->section_offset;
845                 vaddr = reloc->section->parent->virt_offset;
846                 vaddr += reloc->section->cur_offset;
847                 vaddr += reloc->section_offset;
848         } else {
849                 data = reloc->section->data;
850                 data += reloc->section_offset;
851                 vaddr = reloc->section->virt_offset;
852                 vaddr += reloc->section_offset;
853         }
854
855         *out_start_val = start_val;
856         *out_end_val = end_val;
857         *out_data = data;
858         *out_vaddr = vaddr;
859 }
860
861 #ifdef USE_ELF_RELA
862
863 static ElfRelocA*
864 resolve_relocations (MonoImageWriter *acfg)
865 {
866         BinReloc *reloc;
867         guint8 *data;
868         gsize end_val, start_val;
869         ElfRelocA *rr;
870         int i;
871         gsize vaddr;
872
873         rr = g_new0 (ElfRelocA, acfg->num_relocs);
874         i = 0;
875
876         for (reloc = acfg->relocations; reloc; reloc = reloc->next) {
877                 resolve_reloc (acfg, reloc, &data, &vaddr, &start_val, &end_val);
878                 /* FIXME: little endian */
879                 data [0] = end_val;
880                 data [1] = end_val >> 8;
881                 data [2] = end_val >> 16;
882                 data [3] = end_val >> 24;
883                 // FIXME:
884                 if (start_val == 0 && reloc->val1 [0] != '.') {
885                         rr [i].r_offset = vaddr;
886                         rr [i].r_info = R_X86_64_RELATIVE;
887                         rr [i].r_addend = end_val;
888                         ++i;
889                         g_assert (i <= acfg->num_relocs);
890                 }
891         }
892         return rr;
893 }
894
895 #else /* USE_ELF_RELA */
896
897 static void
898 do_reloc (MonoImageWriter *acfg, BinReloc *reloc, guint8 *data, gssize addr)
899 {
900 #ifdef TARGET_ARM
901         /*
902          * We use the official ARM relocation types, but implement only the stuff actually
903          * needed by the code we generate.
904          */
905         switch (reloc->reloc_type) {
906         case R_ARM_CALL:
907         case R_ARM_JUMP24: {
908                 guint32 *code = (guint32*)(gpointer)data;
909                 guint32 ins = *code;
910                 int diff = addr;
911
912                 if (reloc->reloc_type == R_ARM_CALL)
913                         /* bl */
914                         g_assert (data [3] == 0xeb);
915                 else
916                         /* b */
917                         g_assert (data [3] == 0xea);
918                 if (diff >= 0 && diff <= 33554431) {
919                         diff >>= 2;
920                         ins = (ins & 0xff000000) | diff;
921                         *code = ins;
922                 } else if (diff <= 0 && diff >= -33554432) {
923                         diff >>= 2;
924                         ins = (ins & 0xff000000) | (diff & ~0xff000000);
925                         *code = ins;
926                 } else {
927                         g_assert_not_reached ();
928                 }
929                 break;
930         }
931         case R_ARM_ALU_PC_G0_NC: {
932                 /* Generated by emit_plt () */
933                 guint8 *code = data;
934                 guint32 val = addr;
935
936                 g_assert (val <= 0xffffff);
937                 if (val & 0xff0000)
938                         ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_PC, (val & 0xFF0000) >> 16, 16);
939                 else
940                         ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_PC, 0, 0);
941                 ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_IP, (val & 0xFF00) >> 8, 24);
942                 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, val & 0xFF);
943                 break;
944         }               
945         default:
946                 g_assert_not_reached ();
947         }
948 #else
949         g_assert_not_reached ();
950 #endif
951 }
952
953 static ElfReloc*
954 resolve_relocations (MonoImageWriter *acfg)
955 {
956         BinReloc *reloc;
957         guint8 *data;
958         gsize end_val, start_val;
959         ElfReloc *rr;
960         int i;
961         gsize vaddr;
962
963         rr = g_new0 (ElfReloc, acfg->num_relocs);
964         i = 0;
965
966         for (reloc = acfg->relocations; reloc; reloc = reloc->next) {
967                 resolve_reloc (acfg, reloc, &data, &vaddr, &start_val, &end_val);
968                 /* FIXME: little endian */
969                 if (reloc->reloc_type) {
970                         /* Must be static */
971                         g_assert (start_val > 0);
972                         do_reloc (acfg, reloc, data, end_val);
973                 } else {
974                         data [0] = end_val;
975                         data [1] = end_val >> 8;
976                         data [2] = end_val >> 16;
977                         data [3] = end_val >> 24;
978                 }
979                 // FIXME:
980                 if (start_val == 0 && reloc->val1 [0] != '.') {
981                         rr [i].r_offset = vaddr;
982                         rr [i].r_info = R_386_RELATIVE;
983                         ++i;
984                         g_assert (i <= acfg->num_relocs);
985                 }
986         }
987         return rr;
988 }
989
990 #endif /* USE_ELF_RELA */
991
992 static int
993 bin_writer_emit_writeout (MonoImageWriter *acfg)
994 {
995         FILE *file;
996         ElfHeader header;
997         ElfProgHeader progh [3];
998         ElfSectHeader secth [SECT_NUM];
999 #ifdef USE_ELF_RELA
1000         ElfRelocA *relocs;
1001 #else
1002         ElfReloc *relocs;
1003 #endif
1004         ElfStrTable str_table = {NULL, NULL};
1005         ElfStrTable sh_str_table = {NULL, NULL};
1006         ElfStrTable dyn_str_table = {NULL, NULL};
1007         BinSection* all_sections [32];
1008         BinSection* sections [SECT_NUM];
1009         ElfSymbol *dynsym;
1010         ElfSymbol *symtab;
1011         ElfDynamic dynamic [14];
1012         int *hash;
1013         int i, num_sections, file_offset, virt_offset, size, num_symtab;
1014         int num_local_syms;
1015
1016         file = acfg->fp;
1017
1018         /* Section headers */
1019         memset (&secth, 0, sizeof (secth));
1020         memset (&dynamic, 0, sizeof (dynamic));
1021         memset (&header, 0, sizeof (header));
1022
1023         for (i = 1; i < SECT_NUM; ++i) {
1024                 secth [i].sh_name = str_table_add (&sh_str_table, section_info [i].name);
1025                 secth [i].sh_type = section_info [i].type;
1026                 secth [i].sh_addralign = section_info [i].align;
1027                 secth [i].sh_flags = section_info [i].flags;
1028                 secth [i].sh_entsize = section_info [i].esize;
1029         }
1030         secth [SECT_DYNSYM].sh_info = SIZEOF_VOID_P == 4 ? 4 : 2;
1031         secth [SECT_SYMTAB].sh_info = SIZEOF_VOID_P == 4 ? 20 : 17;
1032         secth [SECT_HASH].sh_link = SECT_DYNSYM;
1033         secth [SECT_DYNSYM].sh_link = SECT_DYNSTR;
1034         secth [SECT_REL_DYN].sh_link = SECT_DYNSYM;
1035         secth [SECT_RELA_DYN].sh_link = SECT_DYNSYM;
1036         secth [SECT_DYNAMIC].sh_link = SECT_DYNSTR;
1037         secth [SECT_SYMTAB].sh_link = SECT_STRTAB;
1038
1039         num_sections = collect_sections (acfg, secth, all_sections, 16);
1040         hash = build_hash (acfg, num_sections, &dyn_str_table);
1041         num_symtab = hash [1]; /* FIXME */
1042 #if 0
1043         g_print ("num_sections: %d\n", num_sections);
1044         g_print ("dynsym: %d, dynstr size: %d\n", hash [1], (int)dyn_str_table.data->len);
1045         for (i = 0; i < num_sections; ++i) {
1046                 g_print ("section %s, size: %d, %x\n", all_sections [i]->name, all_sections [i]->cur_offset, all_sections [i]->cur_offset);
1047         }
1048 #endif
1049         /* Associate the bin sections with the ELF sections */
1050         memset (sections, 0, sizeof (sections));
1051         for (i = 0; i < num_sections; ++i) {
1052                 BinSection *sect = all_sections [i];
1053                 int j;
1054
1055                 for (j = 0; j < SECT_NUM; ++j) {
1056                         if (strcmp (sect->name, section_info [j].name) == 0) {
1057                                 sect->shidx = j;
1058                                 break;
1059                         }
1060                 }
1061
1062                 sections [all_sections [i]->shidx] = sect;
1063         }
1064
1065         /* at this point we know where in the file the first segment sections go */
1066         dynsym = collect_syms (acfg, hash, &dyn_str_table, NULL, NULL);
1067         num_local_syms = hash [1];
1068         symtab = collect_syms (acfg, NULL, &str_table, secth, &num_local_syms);
1069
1070         file_offset = virt_offset = sizeof (header) + sizeof (progh);
1071         secth [SECT_HASH].sh_addr = secth [SECT_HASH].sh_offset = file_offset;
1072         size = sizeof (int) * (2 + hash [0] + hash [1]);
1073         virt_offset = (file_offset += size);
1074         secth [SECT_HASH].sh_size = size;
1075         secth [SECT_DYNSYM].sh_addr = secth [SECT_DYNSYM].sh_offset = file_offset;
1076         size = sizeof (ElfSymbol) * hash [1];
1077         virt_offset = (file_offset += size);
1078         secth [SECT_DYNSYM].sh_size = size;
1079         secth [SECT_DYNSTR].sh_addr = secth [SECT_DYNSTR].sh_offset = file_offset;
1080         size = dyn_str_table.data->len;
1081         virt_offset = (file_offset += size);
1082         secth [SECT_DYNSTR].sh_size = size;
1083         file_offset += 4-1;
1084         file_offset &= ~(4-1);
1085         secth [SECT_REL_DYN].sh_addr = secth [SECT_REL_DYN].sh_offset = file_offset;
1086 #ifndef USE_ELF_RELA
1087         size = sizeof (ElfReloc) * acfg->num_relocs;
1088 #else
1089         size = 0;
1090 #endif
1091         virt_offset = (file_offset += size);
1092         secth [SECT_REL_DYN].sh_size = size;
1093         secth [SECT_RELA_DYN].sh_addr = secth [SECT_RELA_DYN].sh_offset = file_offset;
1094 #ifdef USE_ELF_RELA
1095         size = sizeof (ElfRelocA) * acfg->num_relocs;
1096 #else
1097         size = 0;
1098 #endif
1099         virt_offset = (file_offset += size);
1100         secth [SECT_RELA_DYN].sh_size = size;
1101
1102         file_offset = ALIGN_TO (file_offset, secth [SECT_TEXT].sh_addralign);
1103         virt_offset = file_offset;
1104         secth [SECT_TEXT].sh_addr = secth [SECT_TEXT].sh_offset = file_offset;
1105         if (sections [SECT_TEXT]) {
1106                 size = sections [SECT_TEXT]->cur_offset;
1107                 secth [SECT_TEXT].sh_size = size;
1108                 file_offset += size;
1109         }
1110
1111         file_offset = ALIGN_TO (file_offset, secth [SECT_DYNAMIC].sh_addralign);
1112         virt_offset = file_offset;
1113
1114         /* .dynamic, .got.plt, .data, .bss here */
1115         /* Have to increase the virt offset since these go to a separate segment */
1116         virt_offset += PAGESIZE;
1117         secth [SECT_DYNAMIC].sh_addr = virt_offset;
1118         secth [SECT_DYNAMIC].sh_offset = file_offset;
1119         size = sizeof (dynamic);
1120         secth [SECT_DYNAMIC].sh_size = size;
1121         file_offset += size;
1122         virt_offset += size;
1123
1124         file_offset = ALIGN_TO (file_offset, secth [SECT_GOT_PLT].sh_addralign);
1125         virt_offset = ALIGN_TO (virt_offset, secth [SECT_GOT_PLT].sh_addralign);
1126         secth [SECT_GOT_PLT].sh_addr = virt_offset;
1127         secth [SECT_GOT_PLT].sh_offset = file_offset;
1128         size = 12;
1129         secth [SECT_GOT_PLT].sh_size = size;
1130         file_offset += size;
1131         virt_offset += size;
1132
1133         file_offset = ALIGN_TO (file_offset, secth [SECT_DATA].sh_addralign);
1134         virt_offset = ALIGN_TO (virt_offset, secth [SECT_DATA].sh_addralign);
1135         secth [SECT_DATA].sh_addr = virt_offset;
1136         secth [SECT_DATA].sh_offset = file_offset;
1137         if (sections [SECT_DATA]) {
1138                 size = sections [SECT_DATA]->cur_offset;
1139                 secth [SECT_DATA].sh_size = size;
1140                 file_offset += size;
1141                 virt_offset += size;
1142         }
1143
1144         file_offset = ALIGN_TO (file_offset, secth [SECT_BSS].sh_addralign);
1145         virt_offset = ALIGN_TO (virt_offset, secth [SECT_BSS].sh_addralign);
1146         secth [SECT_BSS].sh_addr = virt_offset;
1147         secth [SECT_BSS].sh_offset = file_offset;
1148         if (sections [SECT_BSS]) {
1149                 size = sections [SECT_BSS]->cur_offset;
1150                 secth [SECT_BSS].sh_size = size;
1151         }
1152
1153         /* virtual doesn't matter anymore */
1154         file_offset = ALIGN_TO (file_offset, secth [SECT_DEBUG_FRAME].sh_addralign);
1155         secth [SECT_DEBUG_FRAME].sh_offset = file_offset;
1156         if (sections [SECT_DEBUG_FRAME])
1157                 size = sections [SECT_DEBUG_FRAME]->cur_offset;
1158         else
1159                 size = 0;
1160         secth [SECT_DEBUG_FRAME].sh_size = size;
1161         file_offset += size;
1162
1163         secth [SECT_DEBUG_INFO].sh_offset = file_offset;
1164         if (sections [SECT_DEBUG_INFO])
1165                 size = sections [SECT_DEBUG_INFO]->cur_offset;
1166         else
1167                 size = 0;
1168         secth [SECT_DEBUG_INFO].sh_size = size;
1169         file_offset += size;
1170
1171         secth [SECT_DEBUG_ABBREV].sh_offset = file_offset;
1172         if (sections [SECT_DEBUG_ABBREV])
1173                 size = sections [SECT_DEBUG_ABBREV]->cur_offset;
1174         else
1175                 size = 0;
1176         secth [SECT_DEBUG_ABBREV].sh_size = size;
1177         file_offset += size;
1178
1179         secth [SECT_DEBUG_LINE].sh_offset = file_offset;
1180         if (sections [SECT_DEBUG_LINE])
1181                 size = sections [SECT_DEBUG_LINE]->cur_offset;
1182         else
1183                 size = 0;
1184         secth [SECT_DEBUG_LINE].sh_size = size;
1185         file_offset += size;
1186
1187         secth [SECT_DEBUG_LOC].sh_offset = file_offset;
1188         if (sections [SECT_DEBUG_LOC])
1189                 size = sections [SECT_DEBUG_LOC]->cur_offset;
1190         else
1191                 size = 0;
1192         secth [SECT_DEBUG_LOC].sh_size = size;
1193         file_offset += size;
1194
1195         file_offset = ALIGN_TO (file_offset, secth [SECT_SHSTRTAB].sh_addralign);
1196         secth [SECT_SHSTRTAB].sh_offset = file_offset;
1197         size = sh_str_table.data->len;
1198         secth [SECT_SHSTRTAB].sh_size = size;
1199         file_offset += size;
1200
1201         file_offset = ALIGN_TO (file_offset, secth [SECT_SYMTAB].sh_addralign);
1202         secth [SECT_SYMTAB].sh_offset = file_offset;
1203         size = sizeof (ElfSymbol) * num_local_syms;
1204         secth [SECT_SYMTAB].sh_size = size;
1205         file_offset += size;
1206
1207         file_offset = ALIGN_TO (file_offset, secth [SECT_STRTAB].sh_addralign);
1208         secth [SECT_STRTAB].sh_offset = file_offset;
1209         size = str_table.data->len;
1210         secth [SECT_STRTAB].sh_size = size;
1211         file_offset += size;
1212
1213         file_offset += 4-1;
1214         file_offset &= ~(4-1);
1215
1216         header.e_ident [EI_MAG0] = ELFMAG0;
1217         header.e_ident [EI_MAG1] = ELFMAG1;
1218         header.e_ident [EI_MAG2] = ELFMAG2;
1219         header.e_ident [EI_MAG3] = ELFMAG3;
1220         header.e_ident [EI_CLASS] = SIZEOF_VOID_P == 4 ? ELFCLASS32 : ELFCLASS64;
1221         header.e_ident [EI_DATA] = ELFDATA2LSB;
1222         header.e_ident [EI_VERSION] = EV_CURRENT;
1223         header.e_ident [EI_OSABI] = ELFOSABI_NONE;
1224         header.e_ident [EI_ABIVERSION] = 0;
1225         for (i = EI_PAD; i < EI_NIDENT; ++i)
1226                 header.e_ident [i] = 0;
1227
1228         header.e_type = ET_DYN;
1229 #if defined(TARGET_X86)
1230         header.e_machine = EM_386;
1231 #elif defined(TARGET_AMD64)
1232         header.e_machine = EM_X86_64;
1233 #elif defined(TARGET_ARM)
1234         header.e_machine = EM_ARM;
1235 #else
1236         g_assert_not_reached ();
1237 #endif
1238         header.e_version = 1;
1239
1240         header.e_phoff = sizeof (header);
1241         header.e_ehsize = sizeof (header);
1242         header.e_phentsize = sizeof (ElfProgHeader);
1243         header.e_phnum = 3;
1244         header.e_entry = secth [SECT_TEXT].sh_addr;
1245         header.e_shstrndx = SECT_SHSTRTAB;
1246         header.e_shentsize = sizeof (ElfSectHeader);
1247         header.e_shnum = SECT_NUM;
1248         header.e_shoff = file_offset;
1249
1250         /* dynamic data */
1251         i = 0;
1252         dynamic [i].d_tag = DT_HASH;
1253         dynamic [i].d_un.d_val = secth [SECT_HASH].sh_offset;
1254         ++i;
1255         dynamic [i].d_tag = DT_STRTAB;
1256         dynamic [i].d_un.d_val = secth [SECT_DYNSTR].sh_offset;
1257         ++i;
1258         dynamic [i].d_tag = DT_SYMTAB;
1259         dynamic [i].d_un.d_val = secth [SECT_DYNSYM].sh_offset;
1260         ++i;
1261         dynamic [i].d_tag = DT_STRSZ;
1262         dynamic [i].d_un.d_val = dyn_str_table.data->len;
1263         ++i;
1264         dynamic [i].d_tag = DT_SYMENT;
1265         dynamic [i].d_un.d_val = sizeof (ElfSymbol);
1266         ++i;
1267 #ifdef USE_ELF_RELA
1268         dynamic [i].d_tag = DT_RELA;
1269         dynamic [i].d_un.d_val = secth [SECT_RELA_DYN].sh_offset;
1270         ++i;
1271         dynamic [i].d_tag = DT_RELASZ;
1272         dynamic [i].d_un.d_val = secth [SECT_RELA_DYN].sh_size;
1273         ++i;
1274         dynamic [i].d_tag = DT_RELAENT;
1275         dynamic [i].d_un.d_val = sizeof (ElfRelocA);
1276         ++i;
1277 #else
1278         dynamic [i].d_tag = DT_REL;
1279         dynamic [i].d_un.d_val = secth [SECT_REL_DYN].sh_offset;
1280         ++i;
1281         dynamic [i].d_tag = DT_RELSZ;
1282         dynamic [i].d_un.d_val = secth [SECT_REL_DYN].sh_size;
1283         ++i;
1284         dynamic [i].d_tag = DT_RELENT;
1285         dynamic [i].d_un.d_val = sizeof (ElfReloc);
1286         ++i;
1287 #endif
1288         dynamic [i].d_tag = DT_RELCOUNT;
1289         dynamic [i].d_un.d_val = acfg->num_relocs;
1290         ++i;
1291
1292         /* Program header */
1293         memset (&progh, 0, sizeof (progh));
1294         progh [0].p_type = PT_LOAD;
1295         progh [0].p_filesz = progh [0].p_memsz = secth [SECT_DYNAMIC].sh_offset;
1296         progh [0].p_align = 4096;
1297         progh [0].p_flags = 5;
1298
1299         progh [1].p_type = PT_LOAD;
1300         progh [1].p_offset = secth [SECT_DYNAMIC].sh_offset;
1301         progh [1].p_vaddr = progh [1].p_paddr = secth [SECT_DYNAMIC].sh_addr;
1302         progh [1].p_filesz = secth [SECT_BSS].sh_offset  - secth [SECT_DYNAMIC].sh_offset;
1303         progh [1].p_memsz = secth [SECT_BSS].sh_addr + secth [SECT_BSS].sh_size - secth [SECT_DYNAMIC].sh_addr;
1304         progh [1].p_align = 4096;
1305         progh [1].p_flags = 6;
1306
1307         progh [2].p_type = PT_DYNAMIC;
1308         progh [2].p_offset = secth [SECT_DYNAMIC].sh_offset;
1309         progh [2].p_vaddr = progh [2].p_paddr = secth [SECT_DYNAMIC].sh_addr;
1310         progh [2].p_filesz = progh [2].p_memsz = secth [SECT_DYNAMIC].sh_size;
1311         progh [2].p_align = SIZEOF_VOID_P;
1312         progh [2].p_flags = 6;
1313
1314         /* Compute the addresses of the bin sections, so relocation can be done */
1315         for (i = 0; i < SECT_NUM; ++i) {
1316                 if (sections [i]) {
1317                         sections [i]->file_offset = secth [i].sh_offset;
1318                         sections [i]->virt_offset = secth [i].sh_addr;
1319                 }
1320         }
1321
1322         reloc_symbols (acfg, dynsym, secth, &dyn_str_table, TRUE);
1323         reloc_symbols (acfg, symtab, secth, &str_table, FALSE);
1324         relocs = resolve_relocations (acfg);
1325
1326         fwrite (&header, sizeof (header), 1, file);
1327         fwrite (&progh, sizeof (progh), 1, file);
1328         fwrite (hash, sizeof (int) * (hash [0] + hash [1] + 2), 1, file);
1329         fwrite (dynsym, sizeof (ElfSymbol) * hash [1], 1, file);
1330         fwrite (dyn_str_table.data->str, dyn_str_table.data->len, 1, file);
1331         /* .rel.dyn */
1332         fseek (file, secth [SECT_REL_DYN].sh_offset, SEEK_SET);
1333         fwrite (relocs, sizeof (ElfReloc), acfg->num_relocs, file);
1334
1335         /* .rela.dyn */
1336         fseek (file, secth [SECT_RELA_DYN].sh_offset, SEEK_SET);
1337         fwrite (relocs, secth [SECT_RELA_DYN].sh_size, 1, file);
1338
1339         /* .text */
1340         if (sections [SECT_TEXT]) {
1341                 fseek (file, secth [SECT_TEXT].sh_offset, SEEK_SET);
1342                 fwrite (sections [SECT_TEXT]->data, sections [SECT_TEXT]->cur_offset, 1, file);
1343         }
1344         /* .dynamic */
1345         fwrite (dynamic, sizeof (dynamic), 1, file);
1346
1347         /* .got.plt */
1348         size = secth [SECT_DYNAMIC].sh_addr;
1349         fwrite (&size, sizeof (size), 1, file);
1350
1351         /* .data */
1352         if (sections [SECT_DATA]) {
1353                 fseek (file, secth [SECT_DATA].sh_offset, SEEK_SET);
1354                 fwrite (sections [SECT_DATA]->data, sections [SECT_DATA]->cur_offset, 1, file);
1355         }
1356
1357         fseek (file, secth [SECT_DEBUG_FRAME].sh_offset, SEEK_SET);
1358         if (sections [SECT_DEBUG_FRAME])
1359                 fwrite (sections [SECT_DEBUG_FRAME]->data, sections [SECT_DEBUG_FRAME]->cur_offset, 1, file);
1360         fseek (file, secth [SECT_DEBUG_INFO].sh_offset, SEEK_SET);
1361         if (sections [SECT_DEBUG_INFO])
1362                 fwrite (sections [SECT_DEBUG_INFO]->data, sections [SECT_DEBUG_INFO]->cur_offset, 1, file);
1363         fseek (file, secth [SECT_DEBUG_ABBREV].sh_offset, SEEK_SET);
1364         if (sections [SECT_DEBUG_ABBREV])
1365                 fwrite (sections [SECT_DEBUG_ABBREV]->data, sections [SECT_DEBUG_ABBREV]->cur_offset, 1, file);
1366         fseek (file, secth [SECT_DEBUG_LINE].sh_offset, SEEK_SET);
1367         if (sections [SECT_DEBUG_LINE])
1368                 fwrite (sections [SECT_DEBUG_LINE]->data, sections [SECT_DEBUG_LINE]->cur_offset, 1, file);
1369         fseek (file, secth [SECT_DEBUG_LINE].sh_offset, SEEK_SET);
1370         if (sections [SECT_DEBUG_LOC])
1371                 fwrite (sections [SECT_DEBUG_LOC]->data, sections [SECT_DEBUG_LOC]->cur_offset, 1, file);
1372         fseek (file, secth [SECT_SHSTRTAB].sh_offset, SEEK_SET);
1373         fwrite (sh_str_table.data->str, sh_str_table.data->len, 1, file);
1374         fseek (file, secth [SECT_SYMTAB].sh_offset, SEEK_SET);
1375         fwrite (symtab, sizeof (ElfSymbol) * num_local_syms, 1, file);
1376         fseek (file, secth [SECT_STRTAB].sh_offset, SEEK_SET);
1377         fwrite (str_table.data->str, str_table.data->len, 1, file);
1378         /*g_print ("file_offset %d vs %d\n", file_offset, ftell (file));*/
1379         /*g_assert (file_offset >= ftell (file));*/
1380         fseek (file, file_offset, SEEK_SET);
1381         fwrite (&secth, sizeof (secth), 1, file);
1382         fclose (file);
1383
1384         return 0;
1385 }
1386
1387 #endif /* USE_ELF_WRITER */
1388
1389 #endif /* USE_BIN_WRITER */
1390
1391 /* ASM WRITER */
1392
1393 static void
1394 asm_writer_emit_start (MonoImageWriter *acfg)
1395 {
1396 }
1397
1398 static int
1399 asm_writer_emit_writeout (MonoImageWriter *acfg)
1400 {
1401         fclose (acfg->fp);
1402
1403         return 0;
1404 }
1405
1406 static void
1407 asm_writer_emit_unset_mode (MonoImageWriter *acfg)
1408 {
1409         if (acfg->mode == EMIT_NONE)
1410                 return;
1411         fprintf (acfg->fp, "\n");
1412         acfg->mode = EMIT_NONE;
1413 }
1414
1415 static void
1416 asm_writer_emit_section_change (MonoImageWriter *acfg, const char *section_name, int subsection_index)
1417 {
1418         asm_writer_emit_unset_mode (acfg);
1419 #if defined(PLATFORM_WIN32)
1420         fprintf (acfg->fp, ".section %s\n", section_name);
1421 #elif defined(TARGET_ASM_APPLE)
1422         if (strcmp(section_name, ".bss") == 0)
1423                 fprintf (acfg->fp, "%s\n", ".data");
1424         else
1425                 fprintf (acfg->fp, "%s\n", section_name);
1426 #elif defined(TARGET_ARM) || defined(TARGET_POWERPC)
1427         /* ARM gas doesn't seem to like subsections of .bss */
1428         if (!strcmp (section_name, ".text") || !strcmp (section_name, ".data")) {
1429                 fprintf (acfg->fp, "%s %d\n", section_name, subsection_index);
1430         } else {
1431                 fprintf (acfg->fp, ".section \"%s\"\n", section_name);
1432                 fprintf (acfg->fp, ".subsection %d\n", subsection_index);
1433         }
1434 #else
1435         if (!strcmp (section_name, ".text") || !strcmp (section_name, ".data") || !strcmp (section_name, ".bss")) {
1436                 fprintf (acfg->fp, "%s %d\n", section_name, subsection_index);
1437         } else {
1438                 fprintf (acfg->fp, ".section \"%s\"\n", section_name);
1439                 fprintf (acfg->fp, ".subsection %d\n", subsection_index);
1440         }
1441 #endif
1442 }
1443
1444 static void
1445 asm_writer_emit_symbol_type (MonoImageWriter *acfg, const char *name, gboolean func)
1446 {
1447         const char *stype;
1448
1449         if (func)
1450                 stype = "function";
1451         else
1452                 stype = "object";
1453
1454         asm_writer_emit_unset_mode (acfg);
1455 #if defined(TARGET_ASM_APPLE)
1456
1457 #elif defined(TARGET_ARM)
1458         fprintf (acfg->fp, "\t.type %s,#%s\n", name, stype);
1459 #elif defined(PLATFORM_WIN32)
1460
1461 #else
1462         fprintf (acfg->fp, "\t.type %s,@%s\n", name, stype);
1463 #endif
1464 }
1465
1466 static void
1467 asm_writer_emit_global (MonoImageWriter *acfg, const char *name, gboolean func)
1468 {
1469         asm_writer_emit_unset_mode (acfg);
1470 #if  (defined(__ppc__) && defined(TARGET_ASM_APPLE)) || defined(PLATFORM_WIN32)
1471     // mach-o always uses a '_' prefix.
1472         fprintf (acfg->fp, "\t.globl _%s\n", name);
1473 #else
1474         fprintf (acfg->fp, "\t.globl %s\n", name);
1475 #endif
1476
1477         asm_writer_emit_symbol_type (acfg, name, func);
1478 }
1479
1480 static void
1481 asm_writer_emit_local_symbol (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean func)
1482 {
1483         asm_writer_emit_unset_mode (acfg);
1484
1485 #ifndef TARGET_ASM_APPLE
1486         fprintf (acfg->fp, "\t.local %s\n", name);
1487 #endif
1488
1489         asm_writer_emit_symbol_type (acfg, name, func);
1490 }
1491
1492 static void
1493 asm_writer_emit_label (MonoImageWriter *acfg, const char *name)
1494 {
1495         asm_writer_emit_unset_mode (acfg);
1496 #if (defined(__ppc__) && defined(TARGET_ASM_APPLE)) || defined(PLATFORM_WIN32)
1497     // mach-o always uses a '_' prefix.
1498         fprintf (acfg->fp, "_%s:\n", name);
1499 #else
1500         fprintf (acfg->fp, "%s:\n", name);
1501 #endif
1502
1503 #if defined(PLATFORM_WIN32)
1504         /* Emit a normal label too */
1505         fprintf (acfg->fp, "%s:\n", name);
1506 #endif
1507 }
1508
1509 static void
1510 asm_writer_emit_string (MonoImageWriter *acfg, const char *value)
1511 {
1512         asm_writer_emit_unset_mode (acfg);
1513         fprintf (acfg->fp, "\t%s \"%s\"\n", AS_STRING_DIRECTIVE, value);
1514 }
1515
1516 static void
1517 asm_writer_emit_line (MonoImageWriter *acfg)
1518 {
1519         asm_writer_emit_unset_mode (acfg);
1520         fprintf (acfg->fp, "\n");
1521 }
1522
1523 static void 
1524 asm_writer_emit_alignment (MonoImageWriter *acfg, int size)
1525 {
1526         asm_writer_emit_unset_mode (acfg);
1527 #if defined(TARGET_ARM)
1528         fprintf (acfg->fp, "\t.align %d\n", ilog2 (size));
1529 #elif defined(__ppc__) && defined(TARGET_ASM_APPLE)
1530         // the mach-o assembler specifies alignments as powers of 2.
1531         fprintf (acfg->fp, "\t.align %d\t; ilog2\n", ilog2(size));
1532 #elif defined(TARGET_ASM_GAS)
1533         fprintf (acfg->fp, "\t.balign %d\n", size);
1534 #else
1535         fprintf (acfg->fp, "\t.align %d\n", size);
1536 #endif
1537 }
1538
1539 static void
1540 asm_writer_emit_pointer_unaligned (MonoImageWriter *acfg, const char *target)
1541 {
1542         asm_writer_emit_unset_mode (acfg);
1543         fprintf (acfg->fp, "\t%s %s\n", AS_POINTER_DIRECTIVE, target ? target : "0");
1544 }
1545
1546 static void
1547 asm_writer_emit_pointer (MonoImageWriter *acfg, const char *target)
1548 {
1549         asm_writer_emit_unset_mode (acfg);
1550         asm_writer_emit_alignment (acfg, sizeof (gpointer));
1551         asm_writer_emit_pointer_unaligned (acfg, target);
1552 }
1553
1554 static char *byte_to_str;
1555
1556 static void
1557 asm_writer_emit_bytes (MonoImageWriter *acfg, const guint8* buf, int size)
1558 {
1559         int i;
1560         if (acfg->mode != EMIT_BYTE) {
1561                 acfg->mode = EMIT_BYTE;
1562                 acfg->col_count = 0;
1563         }
1564
1565         if (byte_to_str == NULL) {
1566                 byte_to_str = g_new0 (char, 256 * 8);
1567                 for (i = 0; i < 256; ++i) {
1568                         sprintf (byte_to_str + (i * 8), ",%d", i);
1569                 }
1570         }
1571
1572         for (i = 0; i < size; ++i, ++acfg->col_count) {
1573                 if ((acfg->col_count % 32) == 0)
1574                         fprintf (acfg->fp, "\n\t.byte %d", buf [i]);
1575                 else
1576                         fputs (byte_to_str + (buf [i] * 8), acfg->fp);
1577         }
1578 }
1579
1580 static inline void
1581 asm_writer_emit_int16 (MonoImageWriter *acfg, int value)
1582 {
1583         if (acfg->mode != EMIT_WORD) {
1584                 acfg->mode = EMIT_WORD;
1585                 acfg->col_count = 0;
1586         }
1587         if ((acfg->col_count++ % 8) == 0)
1588                 fprintf (acfg->fp, "\n\t%s ", AS_INT16_DIRECTIVE);
1589         else
1590                 fprintf (acfg->fp, ", ");
1591         fprintf (acfg->fp, "%d", value);
1592 }
1593
1594 static inline void
1595 asm_writer_emit_int32 (MonoImageWriter *acfg, int value)
1596 {
1597         if (acfg->mode != EMIT_LONG) {
1598                 acfg->mode = EMIT_LONG;
1599                 acfg->col_count = 0;
1600         }
1601         if ((acfg->col_count++ % 8) == 0)
1602                 fprintf (acfg->fp, "\n\t%s ", AS_INT32_DIRECTIVE);
1603         else
1604                 fprintf (acfg->fp, ",");
1605         fprintf (acfg->fp, "%d", value);
1606 }
1607
1608 static void
1609 asm_writer_emit_symbol_diff (MonoImageWriter *acfg, const char *end, const char* start, int offset)
1610 {
1611         if (acfg->mode != EMIT_LONG) {
1612                 acfg->mode = EMIT_LONG;
1613                 acfg->col_count = 0;
1614         }
1615         if ((acfg->col_count++ % 8) == 0)
1616                 fprintf (acfg->fp, "\n\t%s ", AS_INT32_DIRECTIVE);
1617         else
1618                 fprintf (acfg->fp, ",");
1619         if (offset > 0)
1620                 fprintf (acfg->fp, "%s - %s + %d", end, start, offset);
1621         else if (offset < 0)
1622                 fprintf (acfg->fp, "%s - %s %d", end, start, offset);
1623         else
1624                 fprintf (acfg->fp, "%s - %s", end, start);
1625 }
1626
1627 static void
1628 asm_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
1629 {
1630         asm_writer_emit_unset_mode (acfg);
1631         fprintf (acfg->fp, "\t%s %d\n", AS_SKIP_DIRECTIVE, num);
1632 }
1633
1634 /* EMIT FUNCTIONS */
1635
1636 void
1637 img_writer_emit_start (MonoImageWriter *acfg)
1638 {
1639 #ifdef USE_BIN_WRITER
1640         if (acfg->use_bin_writer)
1641                 bin_writer_emit_start (acfg);
1642         else
1643                 asm_writer_emit_start (acfg);
1644 #else
1645         asm_writer_emit_start (acfg);
1646 #endif
1647 }
1648
1649 void
1650 img_writer_emit_section_change (MonoImageWriter *acfg, const char *section_name, int subsection_index)
1651 {
1652 #ifdef USE_BIN_WRITER
1653         if (acfg->use_bin_writer)
1654                 bin_writer_emit_section_change (acfg, section_name, subsection_index);
1655         else
1656                 asm_writer_emit_section_change (acfg, section_name, subsection_index);
1657 #else
1658         asm_writer_emit_section_change (acfg, section_name, subsection_index);
1659 #endif
1660
1661         acfg->current_section = section_name;
1662         acfg->current_subsection = subsection_index;
1663 }
1664
1665 void
1666 img_writer_emit_push_section (MonoImageWriter *acfg, const char *section_name, int subsection)
1667 {
1668         g_assert (acfg->stack_pos < 16 - 1);
1669         acfg->section_stack [acfg->stack_pos] = acfg->current_section;
1670         acfg->subsection_stack [acfg->stack_pos] = acfg->current_subsection;
1671         acfg->stack_pos ++;
1672
1673         img_writer_emit_section_change (acfg, section_name, subsection);
1674 }
1675
1676 void
1677 img_writer_emit_pop_section (MonoImageWriter *acfg)
1678 {
1679         g_assert (acfg->stack_pos > 0);
1680         acfg->stack_pos --;
1681         img_writer_emit_section_change (acfg, acfg->section_stack [acfg->stack_pos], acfg->subsection_stack [acfg->stack_pos]);
1682 }
1683
1684 void
1685 img_writer_emit_global (MonoImageWriter *acfg, const char *name, gboolean func)
1686 {
1687 #ifdef USE_BIN_WRITER
1688         if (acfg->use_bin_writer)
1689                 bin_writer_emit_global (acfg, name, func);
1690         else
1691                 asm_writer_emit_global (acfg, name, func);
1692 #else
1693         asm_writer_emit_global (acfg, name, func);
1694 #endif
1695 }
1696
1697 void
1698 img_writer_emit_local_symbol (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean func)
1699 {
1700 #ifdef USE_BIN_WRITER
1701         if (acfg->use_bin_writer)
1702                 bin_writer_emit_local_symbol (acfg, name, end_label, func);
1703         else
1704                 asm_writer_emit_local_symbol (acfg, name, end_label, func);
1705 #else
1706         asm_writer_emit_local_symbol (acfg, name, end_label, func);
1707 #endif
1708 }
1709
1710 void
1711 img_writer_emit_label (MonoImageWriter *acfg, const char *name)
1712 {
1713 #ifdef USE_BIN_WRITER
1714         if (acfg->use_bin_writer)
1715                 bin_writer_emit_label (acfg, name);
1716         else
1717                 asm_writer_emit_label (acfg, name);
1718 #else
1719         asm_writer_emit_label (acfg, name);
1720 #endif
1721 }
1722
1723 void
1724 img_writer_emit_bytes (MonoImageWriter *acfg, const guint8* buf, int size)
1725 {
1726 #ifdef USE_BIN_WRITER
1727         if (acfg->use_bin_writer)
1728                 bin_writer_emit_bytes (acfg, buf, size);
1729         else
1730                 asm_writer_emit_bytes (acfg, buf, size);
1731 #else
1732         asm_writer_emit_bytes (acfg, buf, size);
1733 #endif
1734 }
1735
1736 void
1737 img_writer_emit_string (MonoImageWriter *acfg, const char *value)
1738 {
1739 #ifdef USE_BIN_WRITER
1740         if (acfg->use_bin_writer)
1741                 bin_writer_emit_string (acfg, value);
1742         else
1743                 asm_writer_emit_string (acfg, value);
1744 #else
1745         asm_writer_emit_string (acfg, value);
1746 #endif
1747 }
1748
1749 void
1750 img_writer_emit_line (MonoImageWriter *acfg)
1751 {
1752 #ifdef USE_BIN_WRITER
1753         if (acfg->use_bin_writer)
1754                 bin_writer_emit_line (acfg);
1755         else
1756                 asm_writer_emit_line (acfg);
1757 #else
1758                 asm_writer_emit_line (acfg);
1759 #endif
1760 }
1761
1762 void
1763 img_writer_emit_alignment (MonoImageWriter *acfg, int size)
1764 {
1765 #ifdef USE_BIN_WRITER
1766         if (acfg->use_bin_writer)
1767                 bin_writer_emit_alignment (acfg, size);
1768         else
1769                 asm_writer_emit_alignment (acfg, size);
1770 #else
1771         asm_writer_emit_alignment (acfg, size);
1772 #endif
1773 }
1774
1775 void
1776 img_writer_emit_pointer_unaligned (MonoImageWriter *acfg, const char *target)
1777 {
1778 #ifdef USE_BIN_WRITER
1779         if (acfg->use_bin_writer)
1780                 bin_writer_emit_pointer_unaligned (acfg, target);
1781         else
1782                 asm_writer_emit_pointer_unaligned (acfg, target);
1783 #else
1784         asm_writer_emit_pointer_unaligned (acfg, target);
1785 #endif
1786 }
1787
1788 void
1789 img_writer_emit_pointer (MonoImageWriter *acfg, const char *target)
1790 {
1791 #ifdef USE_BIN_WRITER
1792         if (acfg->use_bin_writer)
1793                 bin_writer_emit_pointer (acfg, target);
1794         else
1795                 asm_writer_emit_pointer (acfg, target);
1796 #else
1797         asm_writer_emit_pointer (acfg, target);
1798 #endif
1799 }
1800
1801 void
1802 img_writer_emit_int16 (MonoImageWriter *acfg, int value)
1803 {
1804 #ifdef USE_BIN_WRITER
1805         if (acfg->use_bin_writer)
1806                 bin_writer_emit_int16 (acfg, value);
1807         else
1808                 asm_writer_emit_int16 (acfg, value);
1809 #else
1810         asm_writer_emit_int16 (acfg, value);
1811 #endif
1812 }
1813
1814 void
1815 img_writer_emit_int32 (MonoImageWriter *acfg, int value)
1816 {
1817 #ifdef USE_BIN_WRITER
1818         if (acfg->use_bin_writer)
1819                 bin_writer_emit_int32 (acfg, value);
1820         else
1821                 asm_writer_emit_int32 (acfg, value);
1822 #else
1823         asm_writer_emit_int32 (acfg, value);
1824 #endif
1825 }
1826
1827 void
1828 img_writer_emit_symbol_diff (MonoImageWriter *acfg, const char *end, const char* start, int offset)
1829 {
1830 #ifdef USE_BIN_WRITER
1831         if (acfg->use_bin_writer)
1832                 bin_writer_emit_symbol_diff (acfg, end, start, offset);
1833         else
1834                 asm_writer_emit_symbol_diff (acfg, end, start, offset);
1835 #else
1836         asm_writer_emit_symbol_diff (acfg, end, start, offset);
1837 #endif
1838 }
1839
1840 void
1841 img_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
1842 {
1843 #ifdef USE_BIN_WRITER
1844         if (acfg->use_bin_writer)
1845                 bin_writer_emit_zero_bytes (acfg, num);
1846         else
1847                 asm_writer_emit_zero_bytes (acfg, num);
1848 #else
1849         asm_writer_emit_zero_bytes (acfg, num);
1850 #endif
1851 }
1852
1853 int
1854 img_writer_emit_writeout (MonoImageWriter *acfg)
1855 {
1856 #ifdef USE_BIN_WRITER
1857         if (acfg->use_bin_writer)
1858                 return bin_writer_emit_writeout (acfg);
1859         else
1860                 return asm_writer_emit_writeout (acfg);
1861 #else
1862                 return asm_writer_emit_writeout (acfg);
1863 #endif
1864 }
1865
1866 void
1867 img_writer_emit_byte (MonoImageWriter *acfg, guint8 val)
1868 {
1869         img_writer_emit_bytes (acfg, &val, 1);
1870 }
1871
1872 /* 
1873  * Emit a relocation entry of type RELOC_TYPE against symbol SYMBOL at the current PC.
1874  * Do not advance PC.
1875  */
1876 void
1877 img_writer_emit_reloc (MonoImageWriter *acfg, int reloc_type, const char *symbol, int addend)
1878 {
1879         /* This is only supported by the bin writer */
1880 #ifdef USE_BIN_WRITER
1881         if (acfg->use_bin_writer)
1882                 bin_writer_emit_reloc (acfg, reloc_type, symbol, addend);
1883         else
1884                 g_assert_not_reached ();
1885 #else
1886                 g_assert_not_reached ();
1887 #endif
1888 }
1889
1890 /*
1891  * img_writer_emit_unset_mode:
1892  *
1893  *   Flush buffered data so it is safe to write to the output file from outside this
1894  * module. This is a nop for the binary writer.
1895  */
1896 void
1897 img_writer_emit_unset_mode (MonoImageWriter *acfg)
1898 {
1899         if (!acfg->use_bin_writer)
1900                 asm_writer_emit_unset_mode (acfg);
1901 }
1902
1903 /*
1904  * Return whenever the binary writer is supported on this platform.
1905  */
1906 gboolean
1907 bin_writer_supported (void)
1908 {
1909 #ifdef USE_BIN_WRITER
1910         return TRUE;
1911 #else
1912         return FALSE;
1913 #endif
1914 }
1915
1916 MonoImageWriter*
1917 img_writer_create (FILE *fp, gboolean use_bin_writer)
1918 {
1919         MonoImageWriter *w = g_new0 (MonoImageWriter, 1);
1920         
1921 #ifndef USE_BIN_WRITER
1922         g_assert (!use_bin_writer);
1923 #endif
1924
1925         w->fp = fp;
1926         w->use_bin_writer = use_bin_writer;
1927         w->mempool = mono_mempool_new ();
1928
1929         return w;
1930 }
1931
1932 void
1933 img_writer_destroy (MonoImageWriter *w)
1934 {
1935         // FIXME: Free all the stuff
1936         mono_mempool_destroy (w->mempool);
1937         g_free (w);
1938 }