2 * image-writer.c: Creation of object files or assembly files using the same interface.
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Zoltan Varga (vargaz@gmail.com)
7 * Paolo Molaro (lupus@ximian.com)
9 * (C) 2002 Ximian, Inc.
13 #include <sys/types.h>
32 #include <limits.h> /* for PAGESIZE */
37 #include "image-writer.h"
40 #include <mono/utils/freebsd-elf32.h>
41 #include <mono/utils/freebsd-elf64.h>
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)
51 * The used assembler dialect
52 * TARGET_ASM_APPLE == apple assembler on OSX
53 * TARGET_ASM_GAS == GNU assembler
55 #if !defined(TARGET_ASM_APPLE) && !defined(TARGET_ASM_GAS)
56 #if defined(TARGET_MACH) && !defined(__native_client_codegen__)
57 #define TARGET_ASM_APPLE
59 #define TARGET_ASM_GAS
64 * Defines for the directives used by different assemblers
66 #if defined(TARGET_POWERPC) || defined(TARGET_MACH)
67 #define AS_STRING_DIRECTIVE ".asciz"
69 #define AS_STRING_DIRECTIVE ".string"
72 #define AS_INT32_DIRECTIVE ".long"
73 #define AS_INT64_DIRECTIVE ".quad"
75 #if (defined(TARGET_AMD64) || defined(TARGET_POWERPC64)) && !defined(__mono_ilp32__)
76 #define AS_POINTER_DIRECTIVE ".quad"
77 #elif defined(TARGET_ARM64)
79 #ifdef TARGET_ASM_APPLE
80 #define AS_POINTER_DIRECTIVE ".quad"
82 #define AS_POINTER_DIRECTIVE ".xword"
86 #define AS_POINTER_DIRECTIVE ".long"
89 #if defined(TARGET_ASM_APPLE)
90 #define AS_INT16_DIRECTIVE ".short"
91 #elif defined(TARGET_ASM_GAS)
92 #define AS_INT16_DIRECTIVE ".hword"
94 #define AS_INT16_DIRECTIVE ".word"
97 #if defined(TARGET_ASM_APPLE)
98 #define AS_SKIP_DIRECTIVE ".space"
100 #define AS_SKIP_DIRECTIVE ".skip"
103 #if defined(TARGET_ASM_APPLE)
104 #define AS_GLOBAL_PREFIX "_"
106 #define AS_GLOBAL_PREFIX ""
109 #ifdef TARGET_ASM_APPLE
110 #define AS_TEMP_LABEL_PREFIX "L"
112 #define AS_TEMP_LABEL_PREFIX ".L"
115 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
116 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
117 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
119 #if defined(TARGET_AMD64) && !defined(HOST_WIN32) && !defined(__APPLE__)
120 #define USE_ELF_WRITER 1
121 #define USE_ELF_RELA 1
124 #if defined(TARGET_X86) && !defined(TARGET_WIN32) && !defined(__APPLE__)
125 #define USE_ELF_WRITER 1
128 #if defined(TARGET_ARM) && !defined(TARGET_MACH) && !defined(HOST_WIN32)
129 #define USE_ELF_WRITER 1
132 #if defined(__mips__)
133 #define USE_ELF_WRITER 1
136 #if defined(TARGET_X86) && defined(__APPLE__)
137 //#define USE_MACH_WRITER
140 #if defined(USE_ELF_WRITER) || defined(USE_MACH_WRITER)
141 #define USE_BIN_WRITER 1
144 #ifdef USE_BIN_WRITER
146 typedef struct _BinSymbol BinSymbol;
147 typedef struct _BinReloc BinReloc;
148 typedef struct _BinSection BinSection;
160 struct _MonoImageWriter {
161 MonoMemPool *mempool;
163 gboolean use_bin_writer;
164 const char *current_section;
165 int current_subsection;
166 const char *section_stack [16];
167 int subsection_stack [16];
171 #ifdef USE_BIN_WRITER
173 BinSection *sections;
174 BinSection *cur_section;
175 BinReloc *relocations;
179 int out_buf_size, out_buf_pos;
183 int mode; /* emit mode */
184 int col_count; /* bytes emitted per .byte line */
188 static G_GNUC_UNUSED int
189 ilog2(register int value)
192 while (value & ~0xf) count += 4, value >>= 4;
193 while (value) count++, value >>= 1;
197 #ifdef USE_BIN_WRITER
199 typedef struct _BinLabel BinLabel;
210 BinSection *val2_section;
223 gboolean is_function;
244 bin_writer_emit_start (MonoImageWriter *acfg)
246 acfg->labels = g_hash_table_new (g_str_hash, g_str_equal);
250 bin_writer_emit_section_change (MonoImageWriter *acfg, const char *section_name, int subsection_index)
254 if (acfg->cur_section && acfg->cur_section->subsection == subsection_index
255 && strcmp (acfg->cur_section->name, section_name) == 0)
257 for (section = acfg->sections; section; section = section->next) {
258 if (section->subsection == subsection_index && strcmp (section->name, section_name) == 0) {
259 acfg->cur_section = section;
264 section = g_new0 (BinSection, 1);
265 section->name = g_strdup (section_name);
266 section->subsection = subsection_index;
267 section->next = acfg->sections;
268 acfg->sections = section;
269 acfg->cur_section = section;
274 bin_writer_set_section_addr (MonoImageWriter *acfg, guint64 addr)
276 acfg->cur_section->addr = addr;
277 acfg->cur_section->has_addr = TRUE;
281 bin_writer_emit_symbol_inner (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean is_global, gboolean func)
283 BinSymbol *symbol = g_new0 (BinSymbol, 1);
284 symbol->name = g_strdup (name);
286 symbol->end_label = g_strdup (end_label);
287 symbol->is_function = func;
288 symbol->is_global = is_global;
289 symbol->section = acfg->cur_section;
290 /* FIXME: we align after this call... */
291 symbol->offset = symbol->section->cur_offset;
292 symbol->next = acfg->symbols;
293 acfg->symbols = symbol;
297 bin_writer_emit_global (MonoImageWriter *acfg, const char *name, gboolean func)
299 bin_writer_emit_symbol_inner (acfg, name, NULL, TRUE, func);
303 bin_writer_emit_local_symbol (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean func)
305 bin_writer_emit_symbol_inner (acfg, name, end_label, FALSE, func);
309 bin_writer_emit_label (MonoImageWriter *acfg, const char *name)
311 BinLabel *label = g_new0 (BinLabel, 1);
312 label->name = g_strdup (name);
313 label->section = acfg->cur_section;
314 label->offset = acfg->cur_section->cur_offset;
315 g_hash_table_insert (acfg->labels, label->name, label);
319 bin_writer_emit_ensure_buffer (BinSection *section, int size)
321 int new_offset = section->cur_offset + size;
322 if (new_offset >= section->data_len) {
323 int new_size = section->data_len? section->data_len * 2: 256;
325 while (new_size <= new_offset)
327 data = g_malloc0 (new_size);
328 #ifdef __native_client_codegen__
329 /* for Native Client, fill empty space with HLT instruction */
331 memset(data, 0xf4, new_size);
333 memcpy (data, section->data, section->data_len);
334 g_free (section->data);
335 section->data = data;
336 section->data_len = new_size;
341 bin_writer_emit_bytes (MonoImageWriter *acfg, const guint8* buf, int size)
343 bin_writer_emit_ensure_buffer (acfg->cur_section, size);
344 memcpy (acfg->cur_section->data + acfg->cur_section->cur_offset, buf, size);
345 acfg->cur_section->cur_offset += size;
349 bin_writer_emit_string (MonoImageWriter *acfg, const char *value)
351 int size = strlen (value) + 1;
352 bin_writer_emit_bytes (acfg, (const guint8*)value, size);
356 bin_writer_emit_line (MonoImageWriter *acfg)
358 /* Nothing to do in binary writer */
362 bin_writer_emit_alignment (MonoImageWriter *acfg, int size)
364 int offset = acfg->cur_section->cur_offset;
366 offset += (size - 1);
367 offset &= ~(size - 1);
368 add = offset - acfg->cur_section->cur_offset;
370 bin_writer_emit_ensure_buffer (acfg->cur_section, add);
371 acfg->cur_section->cur_offset += add;
375 #ifdef __native_client_codegen__
377 bin_writer_emit_nacl_call_alignment (MonoImageWriter *acfg) {
378 int offset = acfg->cur_section->cur_offset;
379 int padding = kNaClAlignment - (offset & kNaClAlignmentMask) - kNaClLengthOfCallImm;
380 guint8 padc = '\x90';
382 if (padding < 0) padding += kNaClAlignment;
384 while (padding > 0) {
385 bin_writer_emit_bytes(acfg, &padc, 1);
389 #endif /* __native_client_codegen__ */
392 bin_writer_emit_pointer_unaligned (MonoImageWriter *acfg, const char *target)
397 acfg->cur_section->cur_offset += sizeof (gpointer);
401 reloc = g_new0 (BinReloc, 1);
402 reloc->val1 = g_strdup (target);
403 reloc->section = acfg->cur_section;
404 reloc->section_offset = acfg->cur_section->cur_offset;
405 reloc->next = acfg->relocations;
406 acfg->relocations = reloc;
407 if (strcmp (reloc->section->name, ".data") == 0) {
409 //g_print ("reloc: %s at %d\n", target, acfg->cur_section->cur_offset);
411 acfg->cur_section->cur_offset += sizeof (gpointer);
415 bin_writer_emit_pointer (MonoImageWriter *acfg, const char *target)
417 bin_writer_emit_alignment (acfg, sizeof (gpointer));
418 bin_writer_emit_pointer_unaligned (acfg, target);
422 bin_writer_emit_int16 (MonoImageWriter *acfg, int value)
425 bin_writer_emit_ensure_buffer (acfg->cur_section, 2);
426 data = acfg->cur_section->data + acfg->cur_section->cur_offset;
427 acfg->cur_section->cur_offset += 2;
428 /* FIXME: little endian */
430 data [1] = value >> 8;
434 bin_writer_emit_int32 (MonoImageWriter *acfg, int value)
437 bin_writer_emit_ensure_buffer (acfg->cur_section, 4);
438 data = acfg->cur_section->data + acfg->cur_section->cur_offset;
439 acfg->cur_section->cur_offset += 4;
440 /* FIXME: little endian */
442 data [1] = value >> 8;
443 data [2] = value >> 16;
444 data [3] = value >> 24;
448 create_reloc (MonoImageWriter *acfg, const char *end, const char* start, int offset)
451 reloc = mono_mempool_alloc0 (acfg->mempool, sizeof (BinReloc));
452 reloc->val1 = mono_mempool_strdup (acfg->mempool, end);
453 if (strcmp (start, ".") == 0) {
454 reloc->val2_section = acfg->cur_section;
455 reloc->val2_offset = acfg->cur_section->cur_offset;
457 reloc->val2 = mono_mempool_strdup (acfg->mempool, start);
459 reloc->offset = offset;
460 reloc->section = acfg->cur_section;
461 reloc->section_offset = acfg->cur_section->cur_offset;
462 reloc->next = acfg->relocations;
463 acfg->relocations = reloc;
468 bin_writer_emit_symbol_diff (MonoImageWriter *acfg, const char *end, const char* start, int offset)
470 create_reloc (acfg, end, start, offset);
471 acfg->cur_section->cur_offset += 4;
472 /*if (strcmp (reloc->section->name, ".data") == 0) {
474 g_print ("reloc: %s - %s + %d at %d\n", end, start, offset, acfg->cur_section->cur_offset - 4);
479 * Emit a relocation entry of type RELOC_TYPE against symbol SYMBOL at the current PC.
482 static G_GNUC_UNUSED void
483 bin_writer_emit_reloc (MonoImageWriter *acfg, int reloc_type, const char *symbol, int addend)
485 BinReloc *reloc = create_reloc (acfg, symbol, ".", addend);
486 reloc->reloc_type = reloc_type;
490 bin_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
492 bin_writer_emit_ensure_buffer (acfg->cur_section, num);
493 acfg->cur_section->cur_offset += num;
497 bin_writer_fwrite (MonoImageWriter *acfg, void *val, size_t size, size_t nmemb)
500 fwrite (val, size, nmemb, acfg->fp);
502 g_assert (acfg->out_buf_pos + (size * nmemb) <= acfg->out_buf_size);
503 memcpy (acfg->out_buf + acfg->out_buf_pos, val, size * nmemb);
504 acfg->out_buf_pos += (size * nmemb);
509 bin_writer_fseek (MonoImageWriter *acfg, int offset)
512 fseek (acfg->fp, offset, SEEK_SET);
514 acfg->out_buf_pos = offset;
517 #ifdef USE_MACH_WRITER
520 * This is a minimal implementation designed to support xdebug on 32 bit osx
521 * FIXME: 64 bit support
524 #include <mach-o/loader.h>
527 get_label_addr (MonoImageWriter *acfg, const char *name)
534 lab = g_hash_table_lookup (acfg->labels, name);
536 g_error ("Undefined label: '%s'.\n", name);
537 section = lab->section;
538 offset = lab->offset;
539 if (section->parent) {
540 value = section->parent->virt_offset + section->cur_offset + offset;
542 value = section->virt_offset + offset;
549 resolve_reloc (MonoImageWriter *acfg, BinReloc *reloc, guint8 **out_data, gsize *out_vaddr, gsize *out_start_val, gsize *out_end_val)
552 gssize end_val, start_val;
555 end_val = get_label_addr (acfg, reloc->val1);
557 start_val = get_label_addr (acfg, reloc->val2);
558 } else if (reloc->val2_section) {
559 start_val = reloc->val2_offset;
560 if (reloc->val2_section->parent)
561 start_val += reloc->val2_section->parent->virt_offset + reloc->val2_section->cur_offset;
563 start_val += reloc->val2_section->virt_offset;
567 end_val = end_val - start_val + reloc->offset;
568 if (reloc->section->parent) {
569 data = reloc->section->parent->data;
570 data += reloc->section->cur_offset;
571 data += reloc->section_offset;
572 vaddr = reloc->section->parent->virt_offset;
573 vaddr += reloc->section->cur_offset;
574 vaddr += reloc->section_offset;
576 data = reloc->section->data;
577 data += reloc->section_offset;
578 vaddr = reloc->section->virt_offset;
579 vaddr += reloc->section_offset;
582 *out_start_val = start_val;
583 *out_end_val = end_val;
589 resolve_relocations (MonoImageWriter *acfg)
593 gsize end_val, start_val;
596 /* Only resolve static relocations */
597 for (reloc = acfg->relocations; reloc; reloc = reloc->next) {
598 resolve_reloc (acfg, reloc, &data, &vaddr, &start_val, &end_val);
600 data [1] = end_val >> 8;
601 data [2] = end_val >> 16;
602 data [3] = end_val >> 24;
607 bin_writer_emit_writeout (MonoImageWriter *acfg)
610 int sindex, file_size, nsections, file_offset, vmaddr;
611 struct mach_header header;
612 struct segment_command segment;
613 struct section *sections;
615 /* Assing vm addresses to sections */
618 for (s = acfg->sections; s; s = s->next) {
619 s->virt_offset = vmaddr;
620 vmaddr += s->cur_offset;
624 resolve_relocations (acfg);
628 memset (&header, 0, sizeof (header));
629 header.magic = MH_MAGIC;
630 header.cputype = CPU_TYPE_X86;
631 header.cpusubtype = CPU_SUBTYPE_X86_ALL;
632 header.filetype = MH_OBJECT;
634 header.sizeofcmds = 0;
637 file_offset += sizeof (header);
639 memset (&segment, 0, sizeof (segment));
640 segment.cmd = LC_SEGMENT;
641 segment.cmdsize = sizeof (segment);
642 segment.maxprot = VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE;
643 segment.initprot = VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE;
645 file_offset += sizeof (segment);
646 file_offset += nsections * sizeof (struct section);
648 sections = g_new0 (struct section, nsections);
650 for (s = acfg->sections; s; s = s->next) {
651 s->file_offset = file_offset;
653 /* .debug_line -> __debug_line */
654 sprintf (sections [sindex].sectname, "__%s", s->name + 1);
655 sprintf (sections [sindex].segname, "%s", "__DWARF");
656 sections [sindex].addr = s->virt_offset;
657 sections [sindex].size = s->cur_offset;
658 sections [sindex].offset = s->file_offset;
660 file_offset += s->cur_offset;
663 segment.cmdsize += sizeof (struct section);
669 header.sizeofcmds += segment.cmdsize;
672 file_size = file_offset;
675 acfg->out_buf_size = file_size;
676 acfg->out_buf = g_malloc (acfg->out_buf_size);
679 bin_writer_fwrite (acfg, &header, sizeof (header), 1);
680 bin_writer_fwrite (acfg, &segment, sizeof (segment), 1);
681 bin_writer_fwrite (acfg, sections, sizeof (struct section), nsections);
682 for (s = acfg->sections; s; s = s->next) {
684 g_assert (acfg->out_buf_pos == s->file_offset);
685 bin_writer_fwrite (acfg, s->data, s->cur_offset, 1);
696 #ifdef USE_ELF_WRITER
722 #if SIZEOF_VOID_P == 4
724 typedef Elf32_Ehdr ElfHeader;
725 typedef Elf32_Shdr ElfSectHeader;
726 typedef Elf32_Phdr ElfProgHeader;
727 typedef Elf32_Sym ElfSymbol;
728 typedef Elf32_Rel ElfReloc;
729 typedef Elf32_Rela ElfRelocA;
730 typedef Elf32_Dyn ElfDynamic;
734 typedef Elf64_Ehdr ElfHeader;
735 typedef Elf64_Shdr ElfSectHeader;
736 typedef Elf64_Phdr ElfProgHeader;
737 typedef Elf64_Sym ElfSymbol;
738 typedef Elf64_Rel ElfReloc;
739 typedef Elf64_Rela ElfRelocA;
740 typedef Elf64_Dyn ElfDynamic;
752 static SectInfo section_info [] = {
754 {".hash", SHT_HASH, 4, 2, SIZEOF_VOID_P},
755 {".dynsym", SHT_DYNSYM, sizeof (ElfSymbol), 2, SIZEOF_VOID_P},
756 {".dynstr", SHT_STRTAB, 0, 2, 1},
757 {".rel.dyn", SHT_REL, sizeof (ElfReloc), 2, SIZEOF_VOID_P},
758 {".rela.dyn", SHT_RELA, sizeof (ElfRelocA), 2, SIZEOF_VOID_P},
759 {".text", SHT_PROGBITS, 0, 6, 4096},
760 {".rodata", SHT_PROGBITS, 0, SHF_ALLOC, 4096},
761 {".dynamic", SHT_DYNAMIC, sizeof (ElfDynamic), 3, SIZEOF_VOID_P},
762 {".got.plt", SHT_PROGBITS, SIZEOF_VOID_P, 3, SIZEOF_VOID_P},
763 {".data", SHT_PROGBITS, 0, 3, 8},
764 {".bss", SHT_NOBITS, 0, 3, 8},
765 {".debug_frame", SHT_PROGBITS, 0, 0, 8},
766 {".debug_info", SHT_PROGBITS, 0, 0, 1},
767 {".debug_abbrev", SHT_PROGBITS, 0, 0, 1},
768 {".debug_line", SHT_PROGBITS, 0, 0, 1},
769 {".debug_loc", SHT_PROGBITS, 0, 0, 1},
770 {".shstrtab", SHT_STRTAB, 0, 0, 1},
771 {".symtab", SHT_SYMTAB, sizeof (ElfSymbol), 0, SIZEOF_VOID_P},
772 {".strtab", SHT_STRTAB, 0, 0, 1}
781 str_table_add (ElfStrTable *table, const char* value)
785 table->data = g_string_new_len ("", 1);
786 table->hash = g_hash_table_new (g_str_hash, g_str_equal);
788 idx = GPOINTER_TO_UINT (g_hash_table_lookup (table->hash, value));
791 idx = table->data->len;
792 g_string_append (table->data, value);
793 g_string_append_c (table->data, 0);
794 g_hash_table_insert (table->hash, (void*)value, GUINT_TO_POINTER (idx));
799 append_subsection (MonoImageWriter *acfg, ElfSectHeader *sheaders, BinSection *sect, BinSection *add)
801 int offset = sect->cur_offset;
802 /*offset += (sheaders [sect->shidx].sh_addralign - 1);
803 offset &= ~(sheaders [sect->shidx].sh_addralign - 1);*/
805 * FIXME: we shouldn't align subsections at all, but if we don't then the
806 * stuff inside the subsections which is aligned won't get aligned.
808 if (strcmp (sect->name, ".debug_line") != 0) {
812 bin_writer_emit_ensure_buffer (sect, offset);
813 //g_print ("section %s aligned to %d from %d\n", sect->name, offset, sect->cur_offset);
814 sect->cur_offset = offset;
816 bin_writer_emit_ensure_buffer (sect, add->cur_offset);
817 memcpy (sect->data + sect->cur_offset, add->data, add->cur_offset);
819 sect->cur_offset += add->cur_offset;
820 add->cur_offset = offset; /* it becomes the offset in the parent section */
821 //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);
826 /* merge the subsections */
828 collect_sections (MonoImageWriter *acfg, ElfSectHeader *sheaders, BinSection **out, int num)
830 int i, j, maxs, num_sections;
835 for (sect = acfg->sections; sect; sect = sect->next) {
836 if (sect->subsection == 0) {
837 out [num_sections++] = sect;
838 g_assert (num_sections < num);
840 maxs = MAX (maxs, sect->subsection);
842 for (i = 0; i < num_sections; i++) {
843 for (j = 1; j <= maxs; ++j) {
844 for (sect = acfg->sections; sect; sect = sect->next) {
845 if (sect->subsection == j && strcmp (out [i]->name, sect->name) == 0) {
846 append_subsection (acfg, sheaders, out [i], sect);
855 elf_hash (const unsigned char *name)
857 unsigned long h = 0, g;
859 h = (h << 4) + *name++;
860 if ((g = h & 0xf0000000))
867 #define NUM_BUCKETS 17
870 build_hash (MonoImageWriter *acfg, int num_sections, ElfStrTable *dynstr)
873 int num_symbols = 1 + num_sections + 3;
876 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
877 if (!symbol->is_global)
880 str_table_add (dynstr, symbol->name);
881 /*g_print ("adding sym: %s\n", symbol->name);*/
883 str_table_add (dynstr, "__bss_start");
884 str_table_add (dynstr, "_edata");
885 str_table_add (dynstr, "_end");
887 data = g_new0 (int, num_symbols + 2 + NUM_BUCKETS);
888 data [0] = NUM_BUCKETS;
889 data [1] = num_symbols;
895 get_label_addr (MonoImageWriter *acfg, const char *name)
902 lab = g_hash_table_lookup (acfg->labels, name);
904 g_error ("Undefined label: '%s'.\n", name);
905 section = lab->section;
906 offset = lab->offset;
907 if (section->parent) {
908 value = section->parent->virt_offset + section->cur_offset + offset;
910 value = section->virt_offset + offset;
916 collect_syms (MonoImageWriter *acfg, int *hash, ElfStrTable *strtab, ElfSectHeader *sheaders, int *num_syms)
927 symbols = g_new0 (ElfSymbol, hash [1]);
930 for (symbol = acfg->symbols; symbol; symbol = symbol->next)
933 symbols = g_new0 (ElfSymbol, i + SECT_NUM + 10); /* FIXME */
936 /* the first symbol is undef, all zeroes */
940 for (j = 1; j < SECT_NUM; ++j) {
941 symbols [i].st_info = ELF32_ST_INFO (STB_LOCAL, STT_SECTION);
942 symbols [i].st_shndx = j;
943 symbols [i].st_value = sheaders [j].sh_addr;
947 for (section = acfg->sections; section; section = section->next) {
950 symbols [i].st_info = ELF32_ST_INFO (STB_LOCAL, STT_SECTION);
951 if (strcmp (section->name, ".text") == 0) {
952 symbols [i].st_shndx = SECT_TEXT;
953 section->shidx = SECT_TEXT;
954 section->file_offset = 4096;
955 symbols [i].st_value = section->virt_offset;
956 } else if (strcmp (section->name, ".rodata") == 0) {
957 symbols [i].st_shndx = SECT_RODATA;
958 section->shidx = SECT_RODATA;
959 section->file_offset = 4096;
960 symbols [i].st_value = section->virt_offset;
961 } else if (strcmp (section->name, ".data") == 0) {
962 symbols [i].st_shndx = SECT_DATA;
963 section->shidx = SECT_DATA;
964 section->file_offset = 4096 + 28; /* FIXME */
965 symbols [i].st_value = section->virt_offset;
966 } else if (strcmp (section->name, ".bss") == 0) {
967 symbols [i].st_shndx = SECT_BSS;
968 section->shidx = SECT_BSS;
969 section->file_offset = 4096 + 28 + 8; /* FIXME */
970 symbols [i].st_value = section->virt_offset;
975 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
978 if (!symbol->is_global && hash)
980 symbols [i].st_info = ELF32_ST_INFO (symbol->is_global ? STB_GLOBAL : STB_LOCAL, symbol->is_function? STT_FUNC : STT_OBJECT);
981 symbols [i].st_name = str_table_add (strtab, symbol->name);
982 /*g_print ("sym name %s tabled to %d\n", symbol->name, symbols [i].st_name);*/
983 section = symbol->section;
984 symbols [i].st_shndx = section->parent? section->parent->shidx: section->shidx;
985 lab = g_hash_table_lookup (acfg->labels, symbol->name);
986 offset = lab->offset;
987 if (section->parent) {
988 symbols [i].st_value = section->parent->virt_offset + section->cur_offset + offset;
990 symbols [i].st_value = section->virt_offset + offset;
993 if (symbol->end_label) {
994 BinLabel *elab = g_hash_table_lookup (acfg->labels, symbol->end_label);
996 symbols [i].st_size = elab->offset - lab->offset;
1000 /* add special symbols */
1001 symbols [i].st_name = str_table_add (strtab, "__bss_start");
1002 symbols [i].st_shndx = 0xfff1;
1003 symbols [i].st_info = ELF32_ST_INFO (STB_GLOBAL, 0);
1005 symbols [i].st_name = str_table_add (strtab, "_edata");
1006 symbols [i].st_shndx = 0xfff1;
1007 symbols [i].st_info = ELF32_ST_INFO (STB_GLOBAL, 0);
1009 symbols [i].st_name = str_table_add (strtab, "_end");
1010 symbols [i].st_shndx = 0xfff1;
1011 symbols [i].st_info = ELF32_ST_INFO (STB_GLOBAL, 0);
1017 /* add to hash table */
1020 chain = hash + 2 + hash [0];
1021 for (i = 0; i < hash [1]; ++i) {
1023 /*g_print ("checking %d '%s' (sym %d)\n", symbols [i].st_name, strtab->data->str + symbols [i].st_name, i);*/
1024 if (!symbols [i].st_name)
1026 hashc = elf_hash ((guint8*)strtab->data->str + symbols [i].st_name);
1027 slot = hashc % hash [0];
1028 /*g_print ("hashing '%s' at slot %d (sym %d)\n", strtab->data->str + symbols [i].st_name, slot, i);*/
1029 if (bucket [slot]) {
1030 chain [i] = bucket [slot];
1041 reloc_symbols (MonoImageWriter *acfg, ElfSymbol *symbols, ElfSectHeader *sheaders, ElfStrTable *strtab, gboolean dynamic)
1043 BinSection *section;
1049 for (section = acfg->sections; section; section = section->next) {
1050 if (section->parent)
1052 symbols [i].st_value = sheaders [section->shidx].sh_addr;
1056 for (i = 1; i < SECT_NUM; ++i) {
1057 symbols [i].st_value = sheaders [i].sh_addr;
1060 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
1063 if (dynamic && !symbol->is_global)
1065 section = symbol->section;
1066 lab = g_hash_table_lookup (acfg->labels, symbol->name);
1067 offset = lab->offset;
1068 if (section->parent) {
1069 symbols [i].st_value = sheaders [section->parent->shidx].sh_addr + section->cur_offset + offset;
1071 symbols [i].st_value = sheaders [section->shidx].sh_addr + offset;
1076 symbols [i].st_value = sheaders [SECT_BSS].sh_addr;
1079 symbols [i].st_value = sheaders [SECT_DATA].sh_addr + sheaders [SECT_DATA].sh_size;
1082 symbols [i].st_value = sheaders [SECT_BSS].sh_addr + sheaders [SECT_BSS].sh_size;
1087 resolve_reloc (MonoImageWriter *acfg, BinReloc *reloc, guint8 **out_data, gsize *out_vaddr, gsize *out_start_val, gsize *out_end_val)
1090 gssize end_val, start_val;
1093 end_val = get_label_addr (acfg, reloc->val1);
1095 start_val = get_label_addr (acfg, reloc->val2);
1096 } else if (reloc->val2_section) {
1097 start_val = reloc->val2_offset;
1098 if (reloc->val2_section->parent)
1099 start_val += reloc->val2_section->parent->virt_offset + reloc->val2_section->cur_offset;
1101 start_val += reloc->val2_section->virt_offset;
1105 end_val = end_val - start_val + reloc->offset;
1106 if (reloc->section->parent) {
1107 data = reloc->section->parent->data;
1108 data += reloc->section->cur_offset;
1109 data += reloc->section_offset;
1110 vaddr = reloc->section->parent->virt_offset;
1111 vaddr += reloc->section->cur_offset;
1112 vaddr += reloc->section_offset;
1114 data = reloc->section->data;
1115 data += reloc->section_offset;
1116 vaddr = reloc->section->virt_offset;
1117 vaddr += reloc->section_offset;
1120 *out_start_val = start_val;
1121 *out_end_val = end_val;
1129 resolve_relocations (MonoImageWriter *acfg)
1133 gsize end_val, start_val;
1138 rr = g_new0 (ElfRelocA, acfg->num_relocs);
1141 for (reloc = acfg->relocations; reloc; reloc = reloc->next) {
1142 resolve_reloc (acfg, reloc, &data, &vaddr, &start_val, &end_val);
1143 /* FIXME: little endian */
1145 data [1] = end_val >> 8;
1146 data [2] = end_val >> 16;
1147 data [3] = end_val >> 24;
1149 if (start_val == 0 && reloc->val1 [0] != '.') {
1150 rr [i].r_offset = vaddr;
1151 rr [i].r_info = R_X86_64_RELATIVE;
1152 rr [i].r_addend = end_val;
1154 g_assert (i <= acfg->num_relocs);
1160 #else /* USE_ELF_RELA */
1163 do_reloc (MonoImageWriter *acfg, BinReloc *reloc, guint8 *data, gssize addr)
1167 * We use the official ARM relocation types, but implement only the stuff actually
1168 * needed by the code we generate.
1170 switch (reloc->reloc_type) {
1172 case R_ARM_JUMP24: {
1173 guint32 *code = (guint32*)(gpointer)data;
1174 guint32 ins = *code;
1177 if (reloc->reloc_type == R_ARM_CALL)
1179 g_assert (data [3] == 0xeb);
1182 g_assert (data [3] == 0xea);
1183 if (diff >= 0 && diff <= 33554431) {
1185 ins = (ins & 0xff000000) | diff;
1187 } else if (diff <= 0 && diff >= -33554432) {
1189 ins = (ins & 0xff000000) | (diff & ~0xff000000);
1192 g_assert_not_reached ();
1196 case R_ARM_ALU_PC_G0_NC: {
1197 /* Generated by emit_plt () */
1198 guint8 *code = data;
1201 g_assert (val <= 0xffffff);
1203 ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_PC, (val & 0xFF0000) >> 16, 16);
1205 ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_PC, 0, 0);
1206 ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_IP, (val & 0xFF00) >> 8, 24);
1207 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, val & 0xFF);
1211 g_assert_not_reached ();
1214 g_assert_not_reached ();
1219 resolve_relocations (MonoImageWriter *acfg)
1223 gsize end_val, start_val;
1228 rr = g_new0 (ElfReloc, acfg->num_relocs);
1231 for (reloc = acfg->relocations; reloc; reloc = reloc->next) {
1232 resolve_reloc (acfg, reloc, &data, &vaddr, &start_val, &end_val);
1233 /* FIXME: little endian */
1234 if (reloc->reloc_type) {
1235 /* Must be static */
1236 g_assert (start_val > 0);
1237 do_reloc (acfg, reloc, data, end_val);
1240 data [1] = end_val >> 8;
1241 data [2] = end_val >> 16;
1242 data [3] = end_val >> 24;
1245 if (start_val == 0 && reloc->val1 [0] != '.') {
1246 rr [i].r_offset = vaddr;
1247 rr [i].r_info = R_386_RELATIVE;
1249 g_assert (i <= acfg->num_relocs);
1255 #endif /* USE_ELF_RELA */
1257 static int normal_sections [] = { SECT_DATA, SECT_DEBUG_FRAME, SECT_DEBUG_INFO, SECT_DEBUG_ABBREV, SECT_DEBUG_LINE, SECT_DEBUG_LOC };
1260 bin_writer_emit_writeout (MonoImageWriter *acfg)
1264 ElfProgHeader progh [4];
1265 ElfSectHeader secth [SECT_NUM];
1271 ElfStrTable str_table = {NULL, NULL};
1272 ElfStrTable sh_str_table = {NULL, NULL};
1273 ElfStrTable dyn_str_table = {NULL, NULL};
1274 BinSection* all_sections [32];
1275 BinSection* sections [SECT_NUM];
1278 ElfDynamic dynamic [14];
1280 int i, num_sections, file_offset, virt_offset, size, num_symtab;
1285 /* Section headers */
1286 memset (§h, 0, sizeof (secth));
1287 memset (&dynamic, 0, sizeof (dynamic));
1288 memset (&header, 0, sizeof (header));
1290 for (i = 1; i < SECT_NUM; ++i) {
1291 secth [i].sh_name = str_table_add (&sh_str_table, section_info [i].name);
1292 secth [i].sh_type = section_info [i].type;
1293 secth [i].sh_addralign = section_info [i].align;
1294 secth [i].sh_flags = section_info [i].flags;
1295 secth [i].sh_entsize = section_info [i].esize;
1297 secth [SECT_DYNSYM].sh_info = SIZEOF_VOID_P == 4 ? 4 : 2;
1298 secth [SECT_SYMTAB].sh_info = SIZEOF_VOID_P == 4 ? 20 : 17;
1299 secth [SECT_HASH].sh_link = SECT_DYNSYM;
1300 secth [SECT_DYNSYM].sh_link = SECT_DYNSTR;
1301 secth [SECT_REL_DYN].sh_link = SECT_DYNSYM;
1302 secth [SECT_RELA_DYN].sh_link = SECT_DYNSYM;
1303 secth [SECT_DYNAMIC].sh_link = SECT_DYNSTR;
1304 secth [SECT_SYMTAB].sh_link = SECT_STRTAB;
1306 num_sections = collect_sections (acfg, secth, all_sections, 16);
1307 hash = build_hash (acfg, num_sections, &dyn_str_table);
1308 num_symtab = hash [1]; /* FIXME */
1310 g_print ("num_sections: %d\n", num_sections);
1311 g_print ("dynsym: %d, dynstr size: %d\n", hash [1], (int)dyn_str_table.data->len);
1312 for (i = 0; i < num_sections; ++i) {
1313 g_print ("section %s, size: %d, %x\n", all_sections [i]->name, all_sections [i]->cur_offset, all_sections [i]->cur_offset);
1316 /* Associate the bin sections with the ELF sections */
1317 memset (sections, 0, sizeof (sections));
1318 for (i = 0; i < num_sections; ++i) {
1319 BinSection *sect = all_sections [i];
1322 for (j = 0; j < SECT_NUM; ++j) {
1323 if (strcmp (sect->name, section_info [j].name) == 0) {
1329 sections [all_sections [i]->shidx] = sect;
1332 /* at this point we know where in the file the first segment sections go */
1333 dynsym = collect_syms (acfg, hash, &dyn_str_table, NULL, NULL);
1334 num_local_syms = hash [1];
1335 symtab = collect_syms (acfg, NULL, &str_table, secth, &num_local_syms);
1337 file_offset = virt_offset = sizeof (header) + sizeof (progh);
1338 secth [SECT_HASH].sh_addr = secth [SECT_HASH].sh_offset = file_offset;
1339 size = sizeof (int) * (2 + hash [0] + hash [1]);
1340 virt_offset = (file_offset += size);
1341 secth [SECT_HASH].sh_size = size;
1342 secth [SECT_DYNSYM].sh_addr = secth [SECT_DYNSYM].sh_offset = file_offset;
1343 size = sizeof (ElfSymbol) * hash [1];
1344 virt_offset = (file_offset += size);
1345 secth [SECT_DYNSYM].sh_size = size;
1346 secth [SECT_DYNSTR].sh_addr = secth [SECT_DYNSTR].sh_offset = file_offset;
1347 size = dyn_str_table.data->len;
1348 virt_offset = (file_offset += size);
1349 secth [SECT_DYNSTR].sh_size = size;
1351 file_offset &= ~(4-1);
1352 secth [SECT_REL_DYN].sh_addr = secth [SECT_REL_DYN].sh_offset = file_offset;
1353 #ifndef USE_ELF_RELA
1354 size = sizeof (ElfReloc) * acfg->num_relocs;
1358 virt_offset = (file_offset += size);
1359 secth [SECT_REL_DYN].sh_size = size;
1360 secth [SECT_RELA_DYN].sh_addr = secth [SECT_RELA_DYN].sh_offset = file_offset;
1362 size = sizeof (ElfRelocA) * acfg->num_relocs;
1366 virt_offset = (file_offset += size);
1367 secth [SECT_RELA_DYN].sh_size = size;
1369 file_offset = ALIGN_TO (file_offset, secth [SECT_TEXT].sh_addralign);
1370 virt_offset = file_offset;
1371 secth [SECT_TEXT].sh_addr = secth [SECT_TEXT].sh_offset = file_offset;
1372 if (sections [SECT_TEXT]) {
1373 if (sections [SECT_TEXT]->has_addr) {
1374 secth [SECT_TEXT].sh_addr = sections [SECT_TEXT]->addr;
1375 secth [SECT_TEXT].sh_flags &= ~SHF_ALLOC;
1377 size = sections [SECT_TEXT]->cur_offset;
1378 secth [SECT_TEXT].sh_size = size;
1379 file_offset += size;
1382 file_offset = ALIGN_TO (file_offset, secth [SECT_RODATA].sh_addralign);
1383 virt_offset = file_offset;
1384 secth [SECT_RODATA].sh_addr = virt_offset;
1385 secth [SECT_RODATA].sh_offset = file_offset;
1386 if (sections [SECT_RODATA]) {
1387 size = sections [SECT_RODATA]->cur_offset;
1388 secth [SECT_RODATA].sh_size = size;
1389 file_offset += size;
1390 virt_offset += size;
1393 file_offset = ALIGN_TO (file_offset, secth [SECT_DYNAMIC].sh_addralign);
1394 virt_offset = file_offset;
1396 /* .dynamic, .got.plt, .data, .bss here */
1397 /* Have to increase the virt offset since these go to a separate segment */
1398 virt_offset += PAGESIZE;
1399 secth [SECT_DYNAMIC].sh_addr = virt_offset;
1400 secth [SECT_DYNAMIC].sh_offset = file_offset;
1401 size = sizeof (dynamic);
1402 secth [SECT_DYNAMIC].sh_size = size;
1403 file_offset += size;
1404 virt_offset += size;
1406 file_offset = ALIGN_TO (file_offset, secth [SECT_GOT_PLT].sh_addralign);
1407 virt_offset = ALIGN_TO (virt_offset, secth [SECT_GOT_PLT].sh_addralign);
1408 secth [SECT_GOT_PLT].sh_addr = virt_offset;
1409 secth [SECT_GOT_PLT].sh_offset = file_offset;
1410 size = 3 * SIZEOF_VOID_P;
1411 secth [SECT_GOT_PLT].sh_size = size;
1412 file_offset += size;
1413 virt_offset += size;
1415 file_offset = ALIGN_TO (file_offset, secth [SECT_DATA].sh_addralign);
1416 virt_offset = ALIGN_TO (virt_offset, secth [SECT_DATA].sh_addralign);
1417 secth [SECT_DATA].sh_addr = virt_offset;
1418 secth [SECT_DATA].sh_offset = file_offset;
1419 if (sections [SECT_DATA]) {
1420 size = sections [SECT_DATA]->cur_offset;
1421 secth [SECT_DATA].sh_size = size;
1422 file_offset += size;
1423 virt_offset += size;
1426 file_offset = ALIGN_TO (file_offset, secth [SECT_BSS].sh_addralign);
1427 virt_offset = ALIGN_TO (virt_offset, secth [SECT_BSS].sh_addralign);
1428 secth [SECT_BSS].sh_addr = virt_offset;
1429 secth [SECT_BSS].sh_offset = file_offset;
1430 if (sections [SECT_BSS]) {
1431 size = sections [SECT_BSS]->cur_offset;
1432 secth [SECT_BSS].sh_size = size;
1435 /* virtual doesn't matter anymore */
1436 file_offset = ALIGN_TO (file_offset, secth [SECT_DEBUG_FRAME].sh_addralign);
1437 secth [SECT_DEBUG_FRAME].sh_offset = file_offset;
1438 if (sections [SECT_DEBUG_FRAME])
1439 size = sections [SECT_DEBUG_FRAME]->cur_offset;
1442 secth [SECT_DEBUG_FRAME].sh_size = size;
1443 file_offset += size;
1445 secth [SECT_DEBUG_INFO].sh_offset = file_offset;
1446 if (sections [SECT_DEBUG_INFO])
1447 size = sections [SECT_DEBUG_INFO]->cur_offset;
1450 secth [SECT_DEBUG_INFO].sh_size = size;
1451 file_offset += size;
1453 secth [SECT_DEBUG_ABBREV].sh_offset = file_offset;
1454 if (sections [SECT_DEBUG_ABBREV])
1455 size = sections [SECT_DEBUG_ABBREV]->cur_offset;
1458 secth [SECT_DEBUG_ABBREV].sh_size = size;
1459 file_offset += size;
1461 secth [SECT_DEBUG_LINE].sh_offset = file_offset;
1462 if (sections [SECT_DEBUG_LINE])
1463 size = sections [SECT_DEBUG_LINE]->cur_offset;
1466 secth [SECT_DEBUG_LINE].sh_size = size;
1467 file_offset += size;
1469 secth [SECT_DEBUG_LOC].sh_offset = file_offset;
1470 if (sections [SECT_DEBUG_LOC])
1471 size = sections [SECT_DEBUG_LOC]->cur_offset;
1474 secth [SECT_DEBUG_LOC].sh_size = size;
1475 file_offset += size;
1477 file_offset = ALIGN_TO (file_offset, secth [SECT_SHSTRTAB].sh_addralign);
1478 secth [SECT_SHSTRTAB].sh_offset = file_offset;
1479 size = sh_str_table.data->len;
1480 secth [SECT_SHSTRTAB].sh_size = size;
1481 file_offset += size;
1483 file_offset = ALIGN_TO (file_offset, secth [SECT_SYMTAB].sh_addralign);
1484 secth [SECT_SYMTAB].sh_offset = file_offset;
1485 size = sizeof (ElfSymbol) * num_local_syms;
1486 secth [SECT_SYMTAB].sh_size = size;
1487 file_offset += size;
1489 file_offset = ALIGN_TO (file_offset, secth [SECT_STRTAB].sh_addralign);
1490 secth [SECT_STRTAB].sh_offset = file_offset;
1491 size = str_table.data->len;
1492 secth [SECT_STRTAB].sh_size = size;
1493 file_offset += size;
1495 for (i = 1; i < SECT_NUM; ++i) {
1496 if (section_info [i].esize != 0)
1497 g_assert (secth [i].sh_size % section_info [i].esize == 0);
1501 file_offset &= ~(4-1);
1503 header.e_ident [EI_MAG0] = ELFMAG0;
1504 header.e_ident [EI_MAG1] = ELFMAG1;
1505 header.e_ident [EI_MAG2] = ELFMAG2;
1506 header.e_ident [EI_MAG3] = ELFMAG3;
1507 header.e_ident [EI_CLASS] = SIZEOF_VOID_P == 4 ? ELFCLASS32 : ELFCLASS64;
1508 header.e_ident [EI_DATA] = ELFDATA2LSB;
1509 header.e_ident [EI_VERSION] = EV_CURRENT;
1510 header.e_ident [EI_OSABI] = ELFOSABI_NONE;
1511 header.e_ident [EI_ABIVERSION] = 0;
1512 for (i = EI_PAD; i < EI_NIDENT; ++i)
1513 header.e_ident [i] = 0;
1515 header.e_type = ET_DYN;
1516 #if defined(TARGET_X86)
1517 header.e_machine = EM_386;
1518 #elif defined(TARGET_AMD64)
1519 header.e_machine = EM_X86_64;
1520 #elif defined(TARGET_ARM)
1521 header.e_machine = EM_ARM;
1523 g_assert_not_reached ();
1525 header.e_version = 1;
1527 header.e_phoff = sizeof (header);
1528 header.e_ehsize = sizeof (header);
1529 header.e_phentsize = sizeof (ElfProgHeader);
1531 header.e_entry = secth [SECT_TEXT].sh_addr;
1532 header.e_shstrndx = SECT_SHSTRTAB;
1533 header.e_shentsize = sizeof (ElfSectHeader);
1534 header.e_shnum = SECT_NUM;
1535 header.e_shoff = file_offset;
1539 dynamic [i].d_tag = DT_HASH;
1540 dynamic [i].d_un.d_val = secth [SECT_HASH].sh_offset;
1542 dynamic [i].d_tag = DT_STRTAB;
1543 dynamic [i].d_un.d_val = secth [SECT_DYNSTR].sh_offset;
1545 dynamic [i].d_tag = DT_SYMTAB;
1546 dynamic [i].d_un.d_val = secth [SECT_DYNSYM].sh_offset;
1548 dynamic [i].d_tag = DT_STRSZ;
1549 dynamic [i].d_un.d_val = dyn_str_table.data->len;
1551 dynamic [i].d_tag = DT_SYMENT;
1552 dynamic [i].d_un.d_val = sizeof (ElfSymbol);
1555 dynamic [i].d_tag = DT_RELA;
1556 dynamic [i].d_un.d_val = secth [SECT_RELA_DYN].sh_offset;
1558 dynamic [i].d_tag = DT_RELASZ;
1559 dynamic [i].d_un.d_val = secth [SECT_RELA_DYN].sh_size;
1561 dynamic [i].d_tag = DT_RELAENT;
1562 dynamic [i].d_un.d_val = sizeof (ElfRelocA);
1565 dynamic [i].d_tag = DT_REL;
1566 dynamic [i].d_un.d_val = secth [SECT_REL_DYN].sh_offset;
1568 dynamic [i].d_tag = DT_RELSZ;
1569 dynamic [i].d_un.d_val = secth [SECT_REL_DYN].sh_size;
1571 dynamic [i].d_tag = DT_RELENT;
1572 dynamic [i].d_un.d_val = sizeof (ElfReloc);
1575 dynamic [i].d_tag = DT_RELCOUNT;
1576 dynamic [i].d_un.d_val = acfg->num_relocs;
1579 /* Program header */
1580 memset (&progh, 0, sizeof (progh));
1581 progh [0].p_type = PT_LOAD;
1582 progh [0].p_filesz = progh [0].p_memsz = secth [SECT_DYNAMIC].sh_offset;
1583 progh [0].p_align = 4096;
1584 progh [0].p_flags = 5;
1586 progh [1].p_type = PT_LOAD;
1587 progh [1].p_offset = secth [SECT_DYNAMIC].sh_offset;
1588 progh [1].p_vaddr = progh [1].p_paddr = secth [SECT_DYNAMIC].sh_addr;
1589 progh [1].p_filesz = secth [SECT_BSS].sh_offset - secth [SECT_DYNAMIC].sh_offset;
1590 progh [1].p_memsz = secth [SECT_BSS].sh_addr + secth [SECT_BSS].sh_size - secth [SECT_DYNAMIC].sh_addr;
1591 progh [1].p_align = 4096;
1592 progh [1].p_flags = 6;
1594 progh [2].p_type = PT_DYNAMIC;
1595 progh [2].p_offset = secth [SECT_DYNAMIC].sh_offset;
1596 progh [2].p_vaddr = progh [2].p_paddr = secth [SECT_DYNAMIC].sh_addr;
1597 progh [2].p_filesz = progh [2].p_memsz = secth [SECT_DYNAMIC].sh_size;
1598 progh [2].p_align = SIZEOF_VOID_P;
1599 progh [2].p_flags = 6;
1601 progh [3].p_type = PT_GNU_STACK;
1602 progh [3].p_offset = secth [SECT_DYNAMIC].sh_offset;
1603 progh [3].p_vaddr = progh [3].p_paddr = secth [SECT_DYNAMIC].sh_addr;
1604 progh [3].p_filesz = progh [3].p_memsz = secth [SECT_DYNAMIC].sh_size;
1605 progh [3].p_align = SIZEOF_VOID_P;
1606 progh [3].p_flags = 6;
1608 /* Compute the addresses of the bin sections, so relocation can be done */
1609 for (i = 0; i < SECT_NUM; ++i) {
1611 sections [i]->file_offset = secth [i].sh_offset;
1612 sections [i]->virt_offset = secth [i].sh_addr;
1616 reloc_symbols (acfg, dynsym, secth, &dyn_str_table, TRUE);
1617 reloc_symbols (acfg, symtab, secth, &str_table, FALSE);
1618 relocs = resolve_relocations (acfg);
1621 acfg->out_buf_size = file_offset + sizeof (secth);
1622 acfg->out_buf = g_malloc (acfg->out_buf_size);
1625 bin_writer_fwrite (acfg, &header, sizeof (header), 1);
1626 bin_writer_fwrite (acfg, &progh, sizeof (progh), 1);
1627 bin_writer_fwrite (acfg, hash, sizeof (int) * (hash [0] + hash [1] + 2), 1);
1628 bin_writer_fwrite (acfg, dynsym, sizeof (ElfSymbol) * hash [1], 1);
1629 bin_writer_fwrite (acfg, dyn_str_table.data->str, dyn_str_table.data->len, 1);
1631 bin_writer_fseek (acfg, secth [SECT_REL_DYN].sh_offset);
1632 bin_writer_fwrite (acfg, relocs, sizeof (ElfReloc), acfg->num_relocs);
1635 bin_writer_fseek (acfg, secth [SECT_RELA_DYN].sh_offset);
1636 bin_writer_fwrite (acfg, relocs, secth [SECT_RELA_DYN].sh_size, 1);
1639 if (sections [SECT_TEXT]) {
1640 bin_writer_fseek (acfg, secth [SECT_TEXT].sh_offset);
1641 bin_writer_fwrite (acfg, sections [SECT_TEXT]->data, sections [SECT_TEXT]->cur_offset, 1);
1644 if (sections [SECT_RODATA]) {
1645 bin_writer_fseek (acfg, secth [SECT_RODATA].sh_offset);
1646 bin_writer_fwrite (acfg, sections [SECT_RODATA]->data, sections [SECT_RODATA]->cur_offset, 1);
1649 bin_writer_fseek (acfg, secth [SECT_DYNAMIC].sh_offset);
1650 bin_writer_fwrite (acfg, dynamic, sizeof (dynamic), 1);
1653 size = secth [SECT_DYNAMIC].sh_addr;
1654 bin_writer_fseek (acfg, secth [SECT_GOT_PLT].sh_offset);
1655 bin_writer_fwrite (acfg, &size, sizeof (size), 1);
1657 /* normal sections */
1658 for (i = 0; i < sizeof (normal_sections) / sizeof (normal_sections [0]); ++i) {
1659 int sect = normal_sections [i];
1661 if (sections [sect]) {
1662 bin_writer_fseek (acfg, secth [sect].sh_offset);
1663 bin_writer_fwrite (acfg, sections [sect]->data, sections [sect]->cur_offset, 1);
1667 bin_writer_fseek (acfg, secth [SECT_SHSTRTAB].sh_offset);
1668 bin_writer_fwrite (acfg, sh_str_table.data->str, sh_str_table.data->len, 1);
1669 bin_writer_fseek (acfg, secth [SECT_SYMTAB].sh_offset);
1670 bin_writer_fwrite (acfg, symtab, sizeof (ElfSymbol) * num_local_syms, 1);
1671 bin_writer_fseek (acfg, secth [SECT_STRTAB].sh_offset);
1672 bin_writer_fwrite (acfg, str_table.data->str, str_table.data->len, 1);
1673 /*g_print ("file_offset %d vs %d\n", file_offset, ftell (file));*/
1674 /*g_assert (file_offset >= ftell (file));*/
1675 bin_writer_fseek (acfg, file_offset);
1676 bin_writer_fwrite (acfg, §h, sizeof (secth), 1);
1684 #endif /* USE_ELF_WRITER */
1686 #endif /* USE_BIN_WRITER */
1691 asm_writer_emit_start (MonoImageWriter *acfg)
1696 asm_writer_emit_writeout (MonoImageWriter *acfg)
1704 asm_writer_emit_unset_mode (MonoImageWriter *acfg)
1706 if (acfg->mode == EMIT_NONE)
1708 fprintf (acfg->fp, "\n");
1709 acfg->mode = EMIT_NONE;
1713 asm_writer_emit_section_change (MonoImageWriter *acfg, const char *section_name, int subsection_index)
1715 asm_writer_emit_unset_mode (acfg);
1716 #if defined(TARGET_ASM_APPLE)
1717 if (strcmp(section_name, ".bss") == 0)
1718 fprintf (acfg->fp, "%s\n", ".data");
1719 else if (strstr (section_name, ".debug") == section_name) {
1720 //g_assert (subsection_index == 0);
1721 fprintf (acfg->fp, ".section __DWARF, __%s,regular,debug\n", section_name + 1);
1723 fprintf (acfg->fp, "%s\n", section_name);
1724 #elif defined(TARGET_ARM) || defined(TARGET_ARM64) || defined(TARGET_POWERPC)
1725 /* ARM gas doesn't seem to like subsections of .bss */
1726 if (!strcmp (section_name, ".text") || !strcmp (section_name, ".data")) {
1727 fprintf (acfg->fp, "%s %d\n", section_name, subsection_index);
1729 fprintf (acfg->fp, ".section \"%s\"\n", section_name);
1730 fprintf (acfg->fp, ".subsection %d\n", subsection_index);
1732 #elif defined(HOST_WIN32)
1733 fprintf (acfg->fp, ".section %s\n", section_name);
1735 if (!strcmp (section_name, ".text") || !strcmp (section_name, ".data") || !strcmp (section_name, ".bss")) {
1736 fprintf (acfg->fp, "%s %d\n", section_name, subsection_index);
1738 fprintf (acfg->fp, ".section \"%s\"\n", section_name);
1739 fprintf (acfg->fp, ".subsection %d\n", subsection_index);
1745 const char *get_label (const char *s)
1747 #ifdef TARGET_ASM_APPLE
1748 if (s [0] == '.' && s [1] == 'L')
1749 /* apple uses "L" instead of ".L" to mark temporary labels */
1756 asm_writer_emit_symbol_type (MonoImageWriter *acfg, const char *name, gboolean func)
1765 asm_writer_emit_unset_mode (acfg);
1766 #if defined(TARGET_ASM_APPLE)
1768 #elif defined(TARGET_ARM)
1769 fprintf (acfg->fp, "\t.type %s,#%s\n", name, stype);
1771 fprintf (acfg->fp, "\t.type %s,@%s\n", name, stype);
1776 asm_writer_emit_global (MonoImageWriter *acfg, const char *name, gboolean func)
1778 asm_writer_emit_unset_mode (acfg);
1780 fprintf (acfg->fp, "\t.globl %s\n", name);
1782 asm_writer_emit_symbol_type (acfg, name, func);
1786 asm_writer_emit_local_symbol (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean func)
1788 asm_writer_emit_unset_mode (acfg);
1790 #ifndef TARGET_ASM_APPLE
1791 fprintf (acfg->fp, "\t.local %s\n", name);
1794 asm_writer_emit_symbol_type (acfg, name, func);
1798 asm_writer_emit_symbol_size (MonoImageWriter *acfg, const char *name, const char *end_label)
1800 asm_writer_emit_unset_mode (acfg);
1802 #ifndef TARGET_ASM_APPLE
1803 fprintf (acfg->fp, "\t.size %s,%s-%s\n", name, end_label, name);
1808 asm_writer_emit_label (MonoImageWriter *acfg, const char *name)
1810 asm_writer_emit_unset_mode (acfg);
1811 fprintf (acfg->fp, "%s:\n", get_label (name));
1815 asm_writer_emit_string (MonoImageWriter *acfg, const char *value)
1817 asm_writer_emit_unset_mode (acfg);
1818 fprintf (acfg->fp, "\t%s \"%s\"\n", AS_STRING_DIRECTIVE, value);
1822 asm_writer_emit_line (MonoImageWriter *acfg)
1824 asm_writer_emit_unset_mode (acfg);
1825 fprintf (acfg->fp, "\n");
1829 asm_writer_emit_alignment (MonoImageWriter *acfg, int size)
1831 asm_writer_emit_unset_mode (acfg);
1832 #if defined(TARGET_ARM)
1833 fprintf (acfg->fp, "\t.align %d\n", ilog2 (size));
1834 #elif defined(__ppc__) && defined(TARGET_ASM_APPLE)
1835 // the mach-o assembler specifies alignments as powers of 2.
1836 fprintf (acfg->fp, "\t.align %d\t; ilog2\n", ilog2(size));
1837 #elif defined(TARGET_ASM_GAS)
1838 fprintf (acfg->fp, "\t.balign %d\n", size);
1839 #elif defined(TARGET_ASM_APPLE)
1840 fprintf (acfg->fp, "\t.align %d\n", ilog2 (size));
1842 fprintf (acfg->fp, "\t.align %d\n", size);
1847 asm_writer_emit_alignment_fill (MonoImageWriter *acfg, int size, int fill)
1849 asm_writer_emit_unset_mode (acfg);
1850 #if defined(TARGET_ASM_APPLE)
1851 fprintf (acfg->fp, "\t.align %d, 0x%0x\n", ilog2 (size), fill);
1853 asm_writer_emit_alignment (acfg, size);
1857 #ifdef __native_client_codegen__
1859 asm_writer_emit_nacl_call_alignment (MonoImageWriter *acfg) {
1860 int padding = kNaClAlignment - kNaClLengthOfCallImm;
1861 guint8 padc = '\x90';
1863 fprintf (acfg->fp, "\n\t.align %d", kNaClAlignment);
1864 while (padding > 0) {
1865 fprintf (acfg->fp, "\n\t.byte %d", padc);
1869 #endif /* __native_client_codegen__ */
1872 asm_writer_emit_pointer_unaligned (MonoImageWriter *acfg, const char *target)
1874 asm_writer_emit_unset_mode (acfg);
1875 fprintf (acfg->fp, "\t%s %s\n", AS_POINTER_DIRECTIVE, target ? target : "0");
1879 asm_writer_emit_pointer (MonoImageWriter *acfg, const char *target)
1881 asm_writer_emit_unset_mode (acfg);
1882 asm_writer_emit_alignment (acfg, sizeof (gpointer));
1883 asm_writer_emit_pointer_unaligned (acfg, target);
1886 static char *byte_to_str;
1889 asm_writer_emit_bytes (MonoImageWriter *acfg, const guint8* buf, int size)
1892 if (acfg->mode != EMIT_BYTE) {
1893 acfg->mode = EMIT_BYTE;
1894 acfg->col_count = 0;
1897 if (byte_to_str == NULL) {
1898 byte_to_str = g_new0 (char, 256 * 8);
1899 for (i = 0; i < 256; ++i) {
1900 sprintf (byte_to_str + (i * 8), ",%d", i);
1904 for (i = 0; i < size; ++i, ++acfg->col_count) {
1905 if ((acfg->col_count % 32) == 0)
1906 fprintf (acfg->fp, "\n\t.byte %d", buf [i]);
1908 fputs (byte_to_str + (buf [i] * 8), acfg->fp);
1913 asm_writer_emit_int16 (MonoImageWriter *acfg, int value)
1915 if (acfg->mode != EMIT_WORD) {
1916 acfg->mode = EMIT_WORD;
1917 acfg->col_count = 0;
1919 if ((acfg->col_count++ % 8) == 0)
1920 fprintf (acfg->fp, "\n\t%s ", AS_INT16_DIRECTIVE);
1922 fprintf (acfg->fp, ", ");
1923 fprintf (acfg->fp, "%d", value);
1927 asm_writer_emit_int32 (MonoImageWriter *acfg, int value)
1929 if (acfg->mode != EMIT_LONG) {
1930 acfg->mode = EMIT_LONG;
1931 acfg->col_count = 0;
1933 if ((acfg->col_count++ % 8) == 0)
1934 fprintf (acfg->fp, "\n\t%s ", AS_INT32_DIRECTIVE);
1936 fprintf (acfg->fp, ",");
1937 fprintf (acfg->fp, "%d", value);
1941 asm_writer_emit_symbol_diff (MonoImageWriter *acfg, const char *end, const char* start, int offset)
1943 #ifdef TARGET_ASM_APPLE
1944 //char symbol [128];
1947 if (acfg->mode != EMIT_LONG) {
1948 acfg->mode = EMIT_LONG;
1949 acfg->col_count = 0;
1952 // FIXME: This doesn't seem to work on the iphone
1954 //#ifdef TARGET_ASM_APPLE
1955 /* The apple assembler needs a separate symbol to be able to handle complex expressions */
1956 sprintf (symbol, "LTMP_SYM%d", acfg->label_gen);
1957 start = get_label (start);
1958 end = get_label (end);
1961 fprintf (acfg->fp, "\n%s=%s - %s + %d", symbol, end, start, offset);
1962 else if (offset < 0)
1963 fprintf (acfg->fp, "\n%s=%s - %s %d", symbol, end, start, offset);
1965 fprintf (acfg->fp, "\n%s=%s - %s", symbol, end, start);
1967 fprintf (acfg->fp, "\n\t%s ", AS_INT32_DIRECTIVE);
1968 fprintf (acfg->fp, "%s", symbol);
1970 start = get_label (start);
1971 end = get_label (end);
1973 if (offset == 0 && strcmp (start, ".") != 0) {
1975 sprintf (symbol, "%sDIFF_SYM%d", AS_TEMP_LABEL_PREFIX, acfg->label_gen);
1977 fprintf (acfg->fp, "\n%s=%s - %s", symbol, end, start);
1978 fprintf (acfg->fp, "\n\t%s ", AS_INT32_DIRECTIVE);
1979 fprintf (acfg->fp, "%s", symbol);
1983 if ((acfg->col_count++ % 8) == 0)
1984 fprintf (acfg->fp, "\n\t%s ", AS_INT32_DIRECTIVE);
1986 fprintf (acfg->fp, ",");
1988 fprintf (acfg->fp, "%s - %s + %d", end, start, offset);
1989 else if (offset < 0)
1990 fprintf (acfg->fp, "%s - %s %d", end, start, offset);
1992 fprintf (acfg->fp, "%s - %s", end, start);
1997 asm_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
1999 asm_writer_emit_unset_mode (acfg);
2000 fprintf (acfg->fp, "\t%s %d\n", AS_SKIP_DIRECTIVE, num);
2003 /* EMIT FUNCTIONS */
2006 img_writer_emit_start (MonoImageWriter *acfg)
2008 #ifdef USE_BIN_WRITER
2009 if (acfg->use_bin_writer)
2010 bin_writer_emit_start (acfg);
2012 asm_writer_emit_start (acfg);
2014 asm_writer_emit_start (acfg);
2019 img_writer_emit_section_change (MonoImageWriter *acfg, const char *section_name, int subsection_index)
2021 #ifdef USE_BIN_WRITER
2022 if (acfg->use_bin_writer)
2023 bin_writer_emit_section_change (acfg, section_name, subsection_index);
2025 asm_writer_emit_section_change (acfg, section_name, subsection_index);
2027 asm_writer_emit_section_change (acfg, section_name, subsection_index);
2030 acfg->current_section = section_name;
2031 acfg->current_subsection = subsection_index;
2035 img_writer_emit_push_section (MonoImageWriter *acfg, const char *section_name, int subsection)
2037 g_assert (acfg->stack_pos < 16 - 1);
2038 acfg->section_stack [acfg->stack_pos] = acfg->current_section;
2039 acfg->subsection_stack [acfg->stack_pos] = acfg->current_subsection;
2042 img_writer_emit_section_change (acfg, section_name, subsection);
2046 img_writer_emit_pop_section (MonoImageWriter *acfg)
2048 g_assert (acfg->stack_pos > 0);
2050 img_writer_emit_section_change (acfg, acfg->section_stack [acfg->stack_pos], acfg->subsection_stack [acfg->stack_pos]);
2054 img_writer_set_section_addr (MonoImageWriter *acfg, guint64 addr)
2056 #ifdef USE_BIN_WRITER
2057 if (!acfg->use_bin_writer)
2060 bin_writer_set_section_addr (acfg, addr);
2067 img_writer_emit_global (MonoImageWriter *acfg, const char *name, gboolean func)
2069 #ifdef USE_BIN_WRITER
2070 if (acfg->use_bin_writer)
2071 bin_writer_emit_global (acfg, name, func);
2073 asm_writer_emit_global (acfg, name, func);
2075 asm_writer_emit_global (acfg, name, func);
2080 img_writer_emit_local_symbol (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean func)
2082 #ifdef USE_BIN_WRITER
2083 if (acfg->use_bin_writer)
2084 bin_writer_emit_local_symbol (acfg, name, end_label, func);
2086 asm_writer_emit_local_symbol (acfg, name, end_label, func);
2088 asm_writer_emit_local_symbol (acfg, name, end_label, func);
2093 img_writer_emit_symbol_size (MonoImageWriter *acfg, const char *name, const char *end_label)
2095 if (!acfg->use_bin_writer)
2096 asm_writer_emit_symbol_size (acfg, name, end_label);
2100 img_writer_emit_label (MonoImageWriter *acfg, const char *name)
2102 #ifdef USE_BIN_WRITER
2103 if (acfg->use_bin_writer)
2104 bin_writer_emit_label (acfg, name);
2106 asm_writer_emit_label (acfg, name);
2108 asm_writer_emit_label (acfg, name);
2113 img_writer_emit_bytes (MonoImageWriter *acfg, const guint8* buf, int size)
2115 #ifdef USE_BIN_WRITER
2116 if (acfg->use_bin_writer)
2117 bin_writer_emit_bytes (acfg, buf, size);
2119 asm_writer_emit_bytes (acfg, buf, size);
2121 asm_writer_emit_bytes (acfg, buf, size);
2126 img_writer_emit_string (MonoImageWriter *acfg, const char *value)
2128 #ifdef USE_BIN_WRITER
2129 if (acfg->use_bin_writer)
2130 bin_writer_emit_string (acfg, value);
2132 asm_writer_emit_string (acfg, value);
2134 asm_writer_emit_string (acfg, value);
2139 img_writer_emit_line (MonoImageWriter *acfg)
2141 #ifdef USE_BIN_WRITER
2142 if (acfg->use_bin_writer)
2143 bin_writer_emit_line (acfg);
2145 asm_writer_emit_line (acfg);
2147 asm_writer_emit_line (acfg);
2152 img_writer_emit_alignment (MonoImageWriter *acfg, int size)
2154 #ifdef USE_BIN_WRITER
2155 if (acfg->use_bin_writer)
2156 bin_writer_emit_alignment (acfg, size);
2158 asm_writer_emit_alignment (acfg, size);
2160 asm_writer_emit_alignment (acfg, size);
2165 img_writer_emit_alignment_fill (MonoImageWriter *acfg, int size, int fill)
2167 #ifdef USE_BIN_WRITER
2168 if (acfg->use_bin_writer)
2169 bin_writer_emit_alignment (acfg, size);
2171 asm_writer_emit_alignment (acfg, size);
2173 asm_writer_emit_alignment_fill (acfg, size, fill);
2177 #ifdef __native_client_codegen__
2179 img_writer_emit_nacl_call_alignment (MonoImageWriter *acfg) {
2180 #ifdef USE_BIN_WRITER
2181 if (acfg->use_bin_writer)
2182 bin_writer_emit_nacl_call_alignment (acfg);
2184 asm_writer_emit_nacl_call_alignment (acfg);
2186 g_assert_not_reached();
2189 #endif /* __native_client_codegen__ */
2192 img_writer_emit_pointer_unaligned (MonoImageWriter *acfg, const char *target)
2194 #ifdef USE_BIN_WRITER
2195 if (acfg->use_bin_writer)
2196 bin_writer_emit_pointer_unaligned (acfg, target);
2198 asm_writer_emit_pointer_unaligned (acfg, target);
2200 asm_writer_emit_pointer_unaligned (acfg, target);
2205 img_writer_emit_pointer (MonoImageWriter *acfg, const char *target)
2207 #ifdef USE_BIN_WRITER
2208 if (acfg->use_bin_writer)
2209 bin_writer_emit_pointer (acfg, target);
2211 asm_writer_emit_pointer (acfg, target);
2213 asm_writer_emit_pointer (acfg, target);
2218 img_writer_emit_int16 (MonoImageWriter *acfg, int value)
2220 #ifdef USE_BIN_WRITER
2221 if (acfg->use_bin_writer)
2222 bin_writer_emit_int16 (acfg, value);
2224 asm_writer_emit_int16 (acfg, value);
2226 asm_writer_emit_int16 (acfg, value);
2231 img_writer_emit_int32 (MonoImageWriter *acfg, int value)
2233 #ifdef USE_BIN_WRITER
2234 if (acfg->use_bin_writer)
2235 bin_writer_emit_int32 (acfg, value);
2237 asm_writer_emit_int32 (acfg, value);
2239 asm_writer_emit_int32 (acfg, value);
2244 img_writer_emit_symbol_diff (MonoImageWriter *acfg, const char *end, const char* start, int offset)
2246 #ifdef USE_BIN_WRITER
2247 if (acfg->use_bin_writer)
2248 bin_writer_emit_symbol_diff (acfg, end, start, offset);
2250 asm_writer_emit_symbol_diff (acfg, end, start, offset);
2252 asm_writer_emit_symbol_diff (acfg, end, start, offset);
2257 img_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
2259 #ifdef USE_BIN_WRITER
2260 if (acfg->use_bin_writer)
2261 bin_writer_emit_zero_bytes (acfg, num);
2263 asm_writer_emit_zero_bytes (acfg, num);
2265 asm_writer_emit_zero_bytes (acfg, num);
2270 img_writer_emit_writeout (MonoImageWriter *acfg)
2272 #ifdef USE_BIN_WRITER
2273 if (acfg->use_bin_writer)
2274 return bin_writer_emit_writeout (acfg);
2276 return asm_writer_emit_writeout (acfg);
2278 return asm_writer_emit_writeout (acfg);
2283 img_writer_emit_byte (MonoImageWriter *acfg, guint8 val)
2285 img_writer_emit_bytes (acfg, &val, 1);
2289 * Emit a relocation entry of type RELOC_TYPE against symbol SYMBOL at the current PC.
2290 * Do not advance PC.
2293 img_writer_emit_reloc (MonoImageWriter *acfg, int reloc_type, const char *symbol, int addend)
2295 /* This is only supported by the bin writer */
2296 #ifdef USE_BIN_WRITER
2297 if (acfg->use_bin_writer)
2298 bin_writer_emit_reloc (acfg, reloc_type, symbol, addend);
2300 g_assert_not_reached ();
2302 g_assert_not_reached ();
2307 * img_writer_emit_unset_mode:
2309 * Flush buffered data so it is safe to write to the output file from outside this
2310 * module. This is a nop for the binary writer.
2313 img_writer_emit_unset_mode (MonoImageWriter *acfg)
2315 if (!acfg->use_bin_writer)
2316 asm_writer_emit_unset_mode (acfg);
2320 * img_writer_get_output:
2322 * Return the output buffer of a binary writer emitting to memory. The returned memory
2323 * is from malloc, and it is owned by the caller.
2326 img_writer_get_output (MonoImageWriter *acfg, guint32 *size)
2328 #ifdef USE_BIN_WRITER
2331 g_assert (acfg->use_bin_writer);
2333 buf = acfg->out_buf;
2334 *size = acfg->out_buf_size;
2335 acfg->out_buf = NULL;
2338 g_assert_not_reached ();
2344 * Return whenever the binary writer is supported on this platform.
2347 bin_writer_supported (void)
2349 #ifdef USE_BIN_WRITER
2357 * img_writer_create:
2359 * Create an image writer writing to FP. If USE_BIN_WRITER is TRUE, FP can be NULL,
2360 * in this case the image writer will write to a memory buffer obtainable by calling
2361 * img_writer_get_output ().
2364 img_writer_create (FILE *fp, gboolean use_bin_writer)
2366 MonoImageWriter *w = g_new0 (MonoImageWriter, 1);
2368 #ifndef USE_BIN_WRITER
2369 g_assert (!use_bin_writer);
2372 if (!use_bin_writer)
2376 w->use_bin_writer = use_bin_writer;
2377 w->mempool = mono_mempool_new ();
2383 img_writer_destroy (MonoImageWriter *w)
2385 // FIXME: Free all the stuff
2386 mono_mempool_destroy (w->mempool);
2391 img_writer_subsections_supported (MonoImageWriter *acfg)
2393 #ifdef TARGET_ASM_APPLE
2394 return acfg->use_bin_writer;
2401 img_writer_get_fp (MonoImageWriter *acfg)
2407 img_writer_get_temp_label_prefix (MonoImageWriter *acfg)
2409 return AS_TEMP_LABEL_PREFIX;