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)
8 * Johan Lorensson (lateralusx.github@gmail.com)
10 * (C) 2002 Ximian, Inc.
14 #include <sys/types.h>
33 #include <limits.h> /* for PAGESIZE */
38 #include "image-writer.h"
41 #include <mono/utils/freebsd-elf32.h>
42 #include <mono/utils/freebsd-elf64.h>
47 #define TV_DECLARE(name) gint64 name
48 #define TV_GETTIME(tv) tv = mono_100ns_ticks ()
49 #define TV_ELAPSED(start,end) (((end) - (start)) / 10)
52 * The used assembler dialect
53 * TARGET_ASM_APPLE == apple assembler on OSX
54 * TARGET_ASM_GAS == GNU assembler
56 #if !defined(TARGET_ASM_APPLE) && !defined(TARGET_ASM_GAS)
57 #if defined(TARGET_MACH) && !defined(__native_client_codegen__)
58 #define TARGET_ASM_APPLE
60 #define TARGET_ASM_GAS
65 * Defines for the directives used by different assemblers
67 #if defined(TARGET_POWERPC) || defined(TARGET_MACH)
68 #define AS_STRING_DIRECTIVE ".asciz"
70 #define AS_STRING_DIRECTIVE ".string"
73 #define AS_INT32_DIRECTIVE ".long"
74 #define AS_INT64_DIRECTIVE ".quad"
76 #if (defined(TARGET_AMD64) || defined(TARGET_POWERPC64)) && !defined(__mono_ilp32__)
77 #define AS_POINTER_DIRECTIVE ".quad"
78 #elif defined(TARGET_ARM64)
80 #ifdef TARGET_ASM_APPLE
81 #define AS_POINTER_DIRECTIVE ".quad"
83 #define AS_POINTER_DIRECTIVE ".xword"
87 #define AS_POINTER_DIRECTIVE ".long"
90 #if defined(TARGET_ASM_APPLE)
91 #define AS_INT16_DIRECTIVE ".short"
92 #elif defined(TARGET_ASM_GAS) && defined(TARGET_WIN32)
93 #define AS_INT16_DIRECTIVE ".word"
94 #elif defined(TARGET_ASM_GAS)
95 #define AS_INT16_DIRECTIVE ".hword"
97 #define AS_INT16_DIRECTIVE ".word"
100 #if defined(TARGET_ASM_APPLE)
101 #define AS_SKIP_DIRECTIVE ".space"
103 #define AS_SKIP_DIRECTIVE ".skip"
106 #if defined(TARGET_ASM_APPLE)
107 #define AS_GLOBAL_PREFIX "_"
109 #define AS_GLOBAL_PREFIX ""
112 #ifdef TARGET_ASM_APPLE
113 #define AS_TEMP_LABEL_PREFIX "L"
115 #define AS_TEMP_LABEL_PREFIX ".L"
118 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
119 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
120 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
122 #if defined(TARGET_AMD64) && !defined(HOST_WIN32) && !defined(__APPLE__)
123 #define USE_ELF_WRITER 1
124 #define USE_ELF_RELA 1
127 #if defined(TARGET_X86) && !defined(HOST_WIN32) && !defined(__APPLE__)
128 #define USE_ELF_WRITER 1
131 #if defined(TARGET_ARM) && !defined(TARGET_MACH) && !defined(HOST_WIN32)
132 //#define USE_ELF_WRITER 1
135 #if defined(__mips__)
136 #define USE_ELF_WRITER 1
139 #if defined(TARGET_X86) && defined(__APPLE__)
140 //#define USE_MACH_WRITER
143 #if defined(USE_ELF_WRITER) || defined(USE_MACH_WRITER)
144 #define USE_BIN_WRITER 1
147 #ifdef USE_BIN_WRITER
149 typedef struct _BinSymbol BinSymbol;
150 typedef struct _BinReloc BinReloc;
151 typedef struct _BinSection BinSection;
163 struct _MonoImageWriter {
164 MonoMemPool *mempool;
166 gboolean use_bin_writer;
167 const char *current_section;
168 int current_subsection;
169 const char *section_stack [16];
170 int subsection_stack [16];
174 #ifdef USE_BIN_WRITER
176 BinSection *sections;
177 BinSection *cur_section;
178 BinReloc *relocations;
182 int out_buf_size, out_buf_pos;
186 int mode; /* emit mode */
187 int col_count; /* bytes emitted per .byte line */
191 static G_GNUC_UNUSED int
192 ilog2(register int value)
195 while (value & ~0xf) count += 4, value >>= 4;
196 while (value) count++, value >>= 1;
200 #ifdef USE_BIN_WRITER
202 typedef struct _BinLabel BinLabel;
213 BinSection *val2_section;
226 gboolean is_function;
247 bin_writer_emit_start (MonoImageWriter *acfg)
249 acfg->labels = g_hash_table_new (g_str_hash, g_str_equal);
253 bin_writer_emit_section_change (MonoImageWriter *acfg, const char *section_name, int subsection_index)
257 if (acfg->cur_section && acfg->cur_section->subsection == subsection_index
258 && strcmp (acfg->cur_section->name, section_name) == 0)
260 for (section = acfg->sections; section; section = section->next) {
261 if (section->subsection == subsection_index && strcmp (section->name, section_name) == 0) {
262 acfg->cur_section = section;
267 section = g_new0 (BinSection, 1);
268 section->name = g_strdup (section_name);
269 section->subsection = subsection_index;
270 section->next = acfg->sections;
271 acfg->sections = section;
272 acfg->cur_section = section;
277 bin_writer_set_section_addr (MonoImageWriter *acfg, guint64 addr)
279 acfg->cur_section->addr = addr;
280 acfg->cur_section->has_addr = TRUE;
284 bin_writer_emit_symbol_inner (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean is_global, gboolean func)
286 BinSymbol *symbol = g_new0 (BinSymbol, 1);
287 symbol->name = g_strdup (name);
289 symbol->end_label = g_strdup (end_label);
290 symbol->is_function = func;
291 symbol->is_global = is_global;
292 symbol->section = acfg->cur_section;
293 /* FIXME: we align after this call... */
294 symbol->offset = symbol->section->cur_offset;
295 symbol->next = acfg->symbols;
296 acfg->symbols = symbol;
300 bin_writer_emit_global (MonoImageWriter *acfg, const char *name, gboolean func)
302 bin_writer_emit_symbol_inner (acfg, name, NULL, TRUE, func);
306 bin_writer_emit_local_symbol (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean func)
308 bin_writer_emit_symbol_inner (acfg, name, end_label, FALSE, func);
312 bin_writer_emit_label (MonoImageWriter *acfg, const char *name)
314 BinLabel *label = g_new0 (BinLabel, 1);
315 label->name = g_strdup (name);
316 label->section = acfg->cur_section;
317 label->offset = acfg->cur_section->cur_offset;
318 g_hash_table_insert (acfg->labels, label->name, label);
322 bin_writer_emit_ensure_buffer (BinSection *section, int size)
324 int new_offset = section->cur_offset + size;
325 if (new_offset >= section->data_len) {
326 int new_size = section->data_len? section->data_len * 2: 256;
328 while (new_size <= new_offset)
330 data = (guint8 *)g_malloc0 (new_size);
331 #ifdef __native_client_codegen__
332 /* for Native Client, fill empty space with HLT instruction */
334 memset(data, 0xf4, new_size);
336 memcpy (data, section->data, section->data_len);
337 g_free (section->data);
338 section->data = data;
339 section->data_len = new_size;
344 bin_writer_emit_bytes (MonoImageWriter *acfg, const guint8* buf, int size)
346 bin_writer_emit_ensure_buffer (acfg->cur_section, size);
347 memcpy (acfg->cur_section->data + acfg->cur_section->cur_offset, buf, size);
348 acfg->cur_section->cur_offset += size;
352 bin_writer_emit_string (MonoImageWriter *acfg, const char *value)
354 int size = strlen (value) + 1;
355 bin_writer_emit_bytes (acfg, (const guint8*)value, size);
359 bin_writer_emit_line (MonoImageWriter *acfg)
361 /* Nothing to do in binary writer */
365 bin_writer_emit_alignment (MonoImageWriter *acfg, int size)
367 int offset = acfg->cur_section->cur_offset;
369 offset += (size - 1);
370 offset &= ~(size - 1);
371 add = offset - acfg->cur_section->cur_offset;
373 bin_writer_emit_ensure_buffer (acfg->cur_section, add);
374 acfg->cur_section->cur_offset += add;
379 bin_writer_emit_pointer_unaligned (MonoImageWriter *acfg, const char *target)
384 acfg->cur_section->cur_offset += sizeof (gpointer);
388 reloc = g_new0 (BinReloc, 1);
389 reloc->val1 = g_strdup (target);
390 reloc->section = acfg->cur_section;
391 reloc->section_offset = acfg->cur_section->cur_offset;
392 reloc->next = acfg->relocations;
393 acfg->relocations = reloc;
394 if (strcmp (reloc->section->name, ".data") == 0) {
396 //g_print ("reloc: %s at %d\n", target, acfg->cur_section->cur_offset);
398 acfg->cur_section->cur_offset += sizeof (gpointer);
402 bin_writer_emit_pointer (MonoImageWriter *acfg, const char *target)
404 bin_writer_emit_alignment (acfg, sizeof (gpointer));
405 bin_writer_emit_pointer_unaligned (acfg, target);
409 bin_writer_emit_int16 (MonoImageWriter *acfg, int value)
412 bin_writer_emit_ensure_buffer (acfg->cur_section, 2);
413 data = acfg->cur_section->data + acfg->cur_section->cur_offset;
414 acfg->cur_section->cur_offset += 2;
415 /* FIXME: little endian */
417 data [1] = value >> 8;
421 bin_writer_emit_int32 (MonoImageWriter *acfg, int value)
424 bin_writer_emit_ensure_buffer (acfg->cur_section, 4);
425 data = acfg->cur_section->data + acfg->cur_section->cur_offset;
426 acfg->cur_section->cur_offset += 4;
427 /* FIXME: little endian */
429 data [1] = value >> 8;
430 data [2] = value >> 16;
431 data [3] = value >> 24;
435 create_reloc (MonoImageWriter *acfg, const char *end, const char* start, int offset)
438 reloc = (BinReloc *)mono_mempool_alloc0 (acfg->mempool, sizeof (BinReloc));
439 reloc->val1 = mono_mempool_strdup (acfg->mempool, end);
440 if (strcmp (start, ".") == 0) {
441 reloc->val2_section = acfg->cur_section;
442 reloc->val2_offset = acfg->cur_section->cur_offset;
444 reloc->val2 = mono_mempool_strdup (acfg->mempool, start);
446 reloc->offset = offset;
447 reloc->section = acfg->cur_section;
448 reloc->section_offset = acfg->cur_section->cur_offset;
449 reloc->next = acfg->relocations;
450 acfg->relocations = reloc;
455 bin_writer_emit_symbol_diff (MonoImageWriter *acfg, const char *end, const char* start, int offset)
457 create_reloc (acfg, end, start, offset);
458 acfg->cur_section->cur_offset += 4;
459 /*if (strcmp (reloc->section->name, ".data") == 0) {
461 g_print ("reloc: %s - %s + %d at %d\n", end, start, offset, acfg->cur_section->cur_offset - 4);
466 * Emit a relocation entry of type RELOC_TYPE against symbol SYMBOL at the current PC.
469 static G_GNUC_UNUSED void
470 bin_writer_emit_reloc (MonoImageWriter *acfg, int reloc_type, const char *symbol, int addend)
472 BinReloc *reloc = create_reloc (acfg, symbol, ".", addend);
473 reloc->reloc_type = reloc_type;
477 bin_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
479 bin_writer_emit_ensure_buffer (acfg->cur_section, num);
480 acfg->cur_section->cur_offset += num;
484 bin_writer_fwrite (MonoImageWriter *acfg, void *val, size_t size, size_t nmemb)
487 fwrite (val, size, nmemb, acfg->fp);
489 g_assert (acfg->out_buf_pos + (size * nmemb) <= acfg->out_buf_size);
490 memcpy (acfg->out_buf + acfg->out_buf_pos, val, size * nmemb);
491 acfg->out_buf_pos += (size * nmemb);
496 bin_writer_fseek (MonoImageWriter *acfg, int offset)
499 fseek (acfg->fp, offset, SEEK_SET);
501 acfg->out_buf_pos = offset;
504 #ifdef USE_MACH_WRITER
507 * This is a minimal implementation designed to support xdebug on 32 bit osx
508 * FIXME: 64 bit support
511 #include <mach-o/loader.h>
514 get_label_addr (MonoImageWriter *acfg, const char *name)
521 lab = g_hash_table_lookup (acfg->labels, name);
523 g_error ("Undefined label: '%s'.\n", name);
524 section = lab->section;
525 offset = lab->offset;
526 if (section->parent) {
527 value = section->parent->virt_offset + section->cur_offset + offset;
529 value = section->virt_offset + offset;
536 resolve_reloc (MonoImageWriter *acfg, BinReloc *reloc, guint8 **out_data, gsize *out_vaddr, gsize *out_start_val, gsize *out_end_val)
539 gssize end_val, start_val;
542 end_val = get_label_addr (acfg, reloc->val1);
544 start_val = get_label_addr (acfg, reloc->val2);
545 } else if (reloc->val2_section) {
546 start_val = reloc->val2_offset;
547 if (reloc->val2_section->parent)
548 start_val += reloc->val2_section->parent->virt_offset + reloc->val2_section->cur_offset;
550 start_val += reloc->val2_section->virt_offset;
554 end_val = end_val - start_val + reloc->offset;
555 if (reloc->section->parent) {
556 data = reloc->section->parent->data;
557 data += reloc->section->cur_offset;
558 data += reloc->section_offset;
559 vaddr = reloc->section->parent->virt_offset;
560 vaddr += reloc->section->cur_offset;
561 vaddr += reloc->section_offset;
563 data = reloc->section->data;
564 data += reloc->section_offset;
565 vaddr = reloc->section->virt_offset;
566 vaddr += reloc->section_offset;
569 *out_start_val = start_val;
570 *out_end_val = end_val;
576 resolve_relocations (MonoImageWriter *acfg)
580 gsize end_val, start_val;
583 /* Only resolve static relocations */
584 for (reloc = acfg->relocations; reloc; reloc = reloc->next) {
585 resolve_reloc (acfg, reloc, &data, &vaddr, &start_val, &end_val);
587 data [1] = end_val >> 8;
588 data [2] = end_val >> 16;
589 data [3] = end_val >> 24;
594 bin_writer_emit_writeout (MonoImageWriter *acfg)
597 int sindex, file_size, nsections, file_offset, vmaddr;
598 struct mach_header header;
599 struct segment_command segment;
600 struct section *sections;
602 /* Assing vm addresses to sections */
605 for (s = acfg->sections; s; s = s->next) {
606 s->virt_offset = vmaddr;
607 vmaddr += s->cur_offset;
611 resolve_relocations (acfg);
615 memset (&header, 0, sizeof (header));
616 header.magic = MH_MAGIC;
617 header.cputype = CPU_TYPE_X86;
618 header.cpusubtype = CPU_SUBTYPE_X86_ALL;
619 header.filetype = MH_OBJECT;
621 header.sizeofcmds = 0;
624 file_offset += sizeof (header);
626 memset (&segment, 0, sizeof (segment));
627 segment.cmd = LC_SEGMENT;
628 segment.cmdsize = sizeof (segment);
629 segment.maxprot = VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE;
630 segment.initprot = VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE;
632 file_offset += sizeof (segment);
633 file_offset += nsections * sizeof (struct section);
635 sections = g_new0 (struct section, nsections);
637 for (s = acfg->sections; s; s = s->next) {
638 s->file_offset = file_offset;
640 /* .debug_line -> __debug_line */
641 sprintf (sections [sindex].sectname, "__%s", s->name + 1);
642 sprintf (sections [sindex].segname, "%s", "__DWARF");
643 sections [sindex].addr = s->virt_offset;
644 sections [sindex].size = s->cur_offset;
645 sections [sindex].offset = s->file_offset;
647 file_offset += s->cur_offset;
650 segment.cmdsize += sizeof (struct section);
656 header.sizeofcmds += segment.cmdsize;
659 file_size = file_offset;
662 acfg->out_buf_size = file_size;
663 acfg->out_buf = g_malloc (acfg->out_buf_size);
666 bin_writer_fwrite (acfg, &header, sizeof (header), 1);
667 bin_writer_fwrite (acfg, &segment, sizeof (segment), 1);
668 bin_writer_fwrite (acfg, sections, sizeof (struct section), nsections);
669 for (s = acfg->sections; s; s = s->next) {
671 g_assert (acfg->out_buf_pos == s->file_offset);
672 bin_writer_fwrite (acfg, s->data, s->cur_offset, 1);
683 #ifdef USE_ELF_WRITER
709 #if SIZEOF_VOID_P == 4
711 typedef Elf32_Ehdr ElfHeader;
712 typedef Elf32_Shdr ElfSectHeader;
713 typedef Elf32_Phdr ElfProgHeader;
714 typedef Elf32_Sym ElfSymbol;
715 typedef Elf32_Rel ElfReloc;
716 typedef Elf32_Rela ElfRelocA;
717 typedef Elf32_Dyn ElfDynamic;
721 typedef Elf64_Ehdr ElfHeader;
722 typedef Elf64_Shdr ElfSectHeader;
723 typedef Elf64_Phdr ElfProgHeader;
724 typedef Elf64_Sym ElfSymbol;
725 typedef Elf64_Rel ElfReloc;
726 typedef Elf64_Rela ElfRelocA;
727 typedef Elf64_Dyn ElfDynamic;
739 static SectInfo section_info [] = {
741 {".hash", SHT_HASH, 4, 2, SIZEOF_VOID_P},
742 {".dynsym", SHT_DYNSYM, sizeof (ElfSymbol), 2, SIZEOF_VOID_P},
743 {".dynstr", SHT_STRTAB, 0, 2, 1},
744 {".rel.dyn", SHT_REL, sizeof (ElfReloc), 2, SIZEOF_VOID_P},
745 {".rela.dyn", SHT_RELA, sizeof (ElfRelocA), 2, SIZEOF_VOID_P},
746 {".text", SHT_PROGBITS, 0, 6, 4096},
747 {".rodata", SHT_PROGBITS, 0, SHF_ALLOC, 4096},
748 {".dynamic", SHT_DYNAMIC, sizeof (ElfDynamic), 3, SIZEOF_VOID_P},
749 {".got.plt", SHT_PROGBITS, SIZEOF_VOID_P, 3, SIZEOF_VOID_P},
750 {".data", SHT_PROGBITS, 0, 3, 8},
751 {".bss", SHT_NOBITS, 0, 3, 8},
752 {".debug_frame", SHT_PROGBITS, 0, 0, 8},
753 {".debug_info", SHT_PROGBITS, 0, 0, 1},
754 {".debug_abbrev", SHT_PROGBITS, 0, 0, 1},
755 {".debug_line", SHT_PROGBITS, 0, 0, 1},
756 {".debug_loc", SHT_PROGBITS, 0, 0, 1},
757 {".shstrtab", SHT_STRTAB, 0, 0, 1},
758 {".symtab", SHT_SYMTAB, sizeof (ElfSymbol), 0, SIZEOF_VOID_P},
759 {".strtab", SHT_STRTAB, 0, 0, 1}
768 str_table_add (ElfStrTable *table, const char* value)
772 table->data = g_string_new_len ("", 1);
773 table->hash = g_hash_table_new (g_str_hash, g_str_equal);
775 idx = GPOINTER_TO_UINT (g_hash_table_lookup (table->hash, value));
778 idx = table->data->len;
779 g_string_append (table->data, value);
780 g_string_append_c (table->data, 0);
781 g_hash_table_insert (table->hash, (void*)value, GUINT_TO_POINTER (idx));
786 append_subsection (MonoImageWriter *acfg, ElfSectHeader *sheaders, BinSection *sect, BinSection *add)
788 int offset = sect->cur_offset;
789 /*offset += (sheaders [sect->shidx].sh_addralign - 1);
790 offset &= ~(sheaders [sect->shidx].sh_addralign - 1);*/
792 * FIXME: we shouldn't align subsections at all, but if we don't then the
793 * stuff inside the subsections which is aligned won't get aligned.
795 if (strcmp (sect->name, ".debug_line") != 0) {
799 bin_writer_emit_ensure_buffer (sect, offset);
800 //g_print ("section %s aligned to %d from %d\n", sect->name, offset, sect->cur_offset);
801 sect->cur_offset = offset;
803 bin_writer_emit_ensure_buffer (sect, add->cur_offset);
804 memcpy (sect->data + sect->cur_offset, add->data, add->cur_offset);
806 sect->cur_offset += add->cur_offset;
807 add->cur_offset = offset; /* it becomes the offset in the parent section */
808 //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);
813 /* merge the subsections */
815 collect_sections (MonoImageWriter *acfg, ElfSectHeader *sheaders, BinSection **out, int num)
817 int i, j, maxs, num_sections;
822 for (sect = acfg->sections; sect; sect = sect->next) {
823 if (sect->subsection == 0) {
824 out [num_sections++] = sect;
825 g_assert (num_sections < num);
827 maxs = MAX (maxs, sect->subsection);
829 for (i = 0; i < num_sections; i++) {
830 for (j = 1; j <= maxs; ++j) {
831 for (sect = acfg->sections; sect; sect = sect->next) {
832 if (sect->subsection == j && strcmp (out [i]->name, sect->name) == 0) {
833 append_subsection (acfg, sheaders, out [i], sect);
842 elf_hash (const unsigned char *name)
844 unsigned long h = 0, g;
846 h = (h << 4) + *name++;
847 if ((g = h & 0xf0000000))
854 #define NUM_BUCKETS 17
857 build_hash (MonoImageWriter *acfg, int num_sections, ElfStrTable *dynstr)
860 int num_symbols = 1 + num_sections + 3;
863 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
864 if (!symbol->is_global)
867 str_table_add (dynstr, symbol->name);
868 /*g_print ("adding sym: %s\n", symbol->name);*/
870 str_table_add (dynstr, "__bss_start");
871 str_table_add (dynstr, "_edata");
872 str_table_add (dynstr, "_end");
874 data = g_new0 (int, num_symbols + 2 + NUM_BUCKETS);
875 data [0] = NUM_BUCKETS;
876 data [1] = num_symbols;
882 get_label_addr (MonoImageWriter *acfg, const char *name)
889 lab = (BinLabel *)g_hash_table_lookup (acfg->labels, name);
891 g_error ("Undefined label: '%s'.\n", name);
892 section = lab->section;
893 offset = lab->offset;
894 if (section->parent) {
895 value = section->parent->virt_offset + section->cur_offset + offset;
897 value = section->virt_offset + offset;
903 collect_syms (MonoImageWriter *acfg, int *hash, ElfStrTable *strtab, ElfSectHeader *sheaders, int *num_syms)
914 symbols = g_new0 (ElfSymbol, hash [1]);
917 for (symbol = acfg->symbols; symbol; symbol = symbol->next)
920 symbols = g_new0 (ElfSymbol, i + SECT_NUM + 10); /* FIXME */
923 /* the first symbol is undef, all zeroes */
927 for (j = 1; j < SECT_NUM; ++j) {
928 symbols [i].st_info = ELF32_ST_INFO (STB_LOCAL, STT_SECTION);
929 symbols [i].st_shndx = j;
930 symbols [i].st_value = sheaders [j].sh_addr;
934 for (section = acfg->sections; section; section = section->next) {
937 symbols [i].st_info = ELF32_ST_INFO (STB_LOCAL, STT_SECTION);
938 if (strcmp (section->name, ".text") == 0) {
939 symbols [i].st_shndx = SECT_TEXT;
940 section->shidx = SECT_TEXT;
941 section->file_offset = 4096;
942 symbols [i].st_value = section->virt_offset;
943 } else if (strcmp (section->name, ".rodata") == 0) {
944 symbols [i].st_shndx = SECT_RODATA;
945 section->shidx = SECT_RODATA;
946 section->file_offset = 4096;
947 symbols [i].st_value = section->virt_offset;
948 } else if (strcmp (section->name, ".data") == 0) {
949 symbols [i].st_shndx = SECT_DATA;
950 section->shidx = SECT_DATA;
951 section->file_offset = 4096 + 28; /* FIXME */
952 symbols [i].st_value = section->virt_offset;
953 } else if (strcmp (section->name, ".bss") == 0) {
954 symbols [i].st_shndx = SECT_BSS;
955 section->shidx = SECT_BSS;
956 section->file_offset = 4096 + 28 + 8; /* FIXME */
957 symbols [i].st_value = section->virt_offset;
962 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
965 if (!symbol->is_global && hash)
967 symbols [i].st_info = ELF32_ST_INFO (symbol->is_global ? STB_GLOBAL : STB_LOCAL, symbol->is_function? STT_FUNC : STT_OBJECT);
968 symbols [i].st_name = str_table_add (strtab, symbol->name);
969 /*g_print ("sym name %s tabled to %d\n", symbol->name, symbols [i].st_name);*/
970 section = symbol->section;
971 symbols [i].st_shndx = section->parent? section->parent->shidx: section->shidx;
972 lab = (BinLabel *)g_hash_table_lookup (acfg->labels, symbol->name);
973 offset = lab->offset;
974 if (section->parent) {
975 symbols [i].st_value = section->parent->virt_offset + section->cur_offset + offset;
977 symbols [i].st_value = section->virt_offset + offset;
980 if (symbol->end_label) {
981 BinLabel *elab = (BinLabel *)g_hash_table_lookup (acfg->labels, symbol->end_label);
983 symbols [i].st_size = elab->offset - lab->offset;
987 /* add special symbols */
988 symbols [i].st_name = str_table_add (strtab, "__bss_start");
989 symbols [i].st_shndx = 0xfff1;
990 symbols [i].st_info = ELF32_ST_INFO (STB_GLOBAL, 0);
992 symbols [i].st_name = str_table_add (strtab, "_edata");
993 symbols [i].st_shndx = 0xfff1;
994 symbols [i].st_info = ELF32_ST_INFO (STB_GLOBAL, 0);
996 symbols [i].st_name = str_table_add (strtab, "_end");
997 symbols [i].st_shndx = 0xfff1;
998 symbols [i].st_info = ELF32_ST_INFO (STB_GLOBAL, 0);
1004 /* add to hash table */
1007 chain = hash + 2 + hash [0];
1008 for (i = 0; i < hash [1]; ++i) {
1010 /*g_print ("checking %d '%s' (sym %d)\n", symbols [i].st_name, strtab->data->str + symbols [i].st_name, i);*/
1011 if (!symbols [i].st_name)
1013 hashc = elf_hash ((guint8*)strtab->data->str + symbols [i].st_name);
1014 slot = hashc % hash [0];
1015 /*g_print ("hashing '%s' at slot %d (sym %d)\n", strtab->data->str + symbols [i].st_name, slot, i);*/
1016 if (bucket [slot]) {
1017 chain [i] = bucket [slot];
1028 reloc_symbols (MonoImageWriter *acfg, ElfSymbol *symbols, ElfSectHeader *sheaders, ElfStrTable *strtab, gboolean dynamic)
1030 BinSection *section;
1036 for (section = acfg->sections; section; section = section->next) {
1037 if (section->parent)
1039 symbols [i].st_value = sheaders [section->shidx].sh_addr;
1043 for (i = 1; i < SECT_NUM; ++i) {
1044 symbols [i].st_value = sheaders [i].sh_addr;
1047 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
1050 if (dynamic && !symbol->is_global)
1052 section = symbol->section;
1053 lab = (BinLabel *)g_hash_table_lookup (acfg->labels, symbol->name);
1054 offset = lab->offset;
1055 if (section->parent) {
1056 symbols [i].st_value = sheaders [section->parent->shidx].sh_addr + section->cur_offset + offset;
1058 symbols [i].st_value = sheaders [section->shidx].sh_addr + offset;
1063 symbols [i].st_value = sheaders [SECT_BSS].sh_addr;
1066 symbols [i].st_value = sheaders [SECT_DATA].sh_addr + sheaders [SECT_DATA].sh_size;
1069 symbols [i].st_value = sheaders [SECT_BSS].sh_addr + sheaders [SECT_BSS].sh_size;
1074 resolve_reloc (MonoImageWriter *acfg, BinReloc *reloc, guint8 **out_data, gsize *out_vaddr, gsize *out_start_val, gsize *out_end_val)
1077 gssize end_val, start_val;
1080 end_val = get_label_addr (acfg, reloc->val1);
1082 start_val = get_label_addr (acfg, reloc->val2);
1083 } else if (reloc->val2_section) {
1084 start_val = reloc->val2_offset;
1085 if (reloc->val2_section->parent)
1086 start_val += reloc->val2_section->parent->virt_offset + reloc->val2_section->cur_offset;
1088 start_val += reloc->val2_section->virt_offset;
1092 end_val = end_val - start_val + reloc->offset;
1093 if (reloc->section->parent) {
1094 data = reloc->section->parent->data;
1095 data += reloc->section->cur_offset;
1096 data += reloc->section_offset;
1097 vaddr = reloc->section->parent->virt_offset;
1098 vaddr += reloc->section->cur_offset;
1099 vaddr += reloc->section_offset;
1101 data = reloc->section->data;
1102 data += reloc->section_offset;
1103 vaddr = reloc->section->virt_offset;
1104 vaddr += reloc->section_offset;
1107 *out_start_val = start_val;
1108 *out_end_val = end_val;
1116 resolve_relocations (MonoImageWriter *acfg)
1120 gsize end_val, start_val;
1125 rr = g_new0 (ElfRelocA, acfg->num_relocs);
1128 for (reloc = acfg->relocations; reloc; reloc = reloc->next) {
1129 resolve_reloc (acfg, reloc, &data, &vaddr, &start_val, &end_val);
1130 /* FIXME: little endian */
1132 data [1] = end_val >> 8;
1133 data [2] = end_val >> 16;
1134 data [3] = end_val >> 24;
1136 if (start_val == 0 && reloc->val1 [0] != '.') {
1137 rr [i].r_offset = vaddr;
1138 rr [i].r_info = R_X86_64_RELATIVE;
1139 rr [i].r_addend = end_val;
1141 g_assert (i <= acfg->num_relocs);
1147 #else /* USE_ELF_RELA */
1150 do_reloc (MonoImageWriter *acfg, BinReloc *reloc, guint8 *data, gssize addr)
1154 * We use the official ARM relocation types, but implement only the stuff actually
1155 * needed by the code we generate.
1157 switch (reloc->reloc_type) {
1159 case R_ARM_JUMP24: {
1160 guint32 *code = (guint32*)(gpointer)data;
1161 guint32 ins = *code;
1164 if (reloc->reloc_type == R_ARM_CALL)
1166 g_assert (data [3] == 0xeb);
1169 g_assert (data [3] == 0xea);
1170 if (diff >= 0 && diff <= 33554431) {
1172 ins = (ins & 0xff000000) | diff;
1174 } else if (diff <= 0 && diff >= -33554432) {
1176 ins = (ins & 0xff000000) | (diff & ~0xff000000);
1179 g_assert_not_reached ();
1183 case R_ARM_ALU_PC_G0_NC: {
1184 /* Generated by emit_plt () */
1185 guint8 *code = data;
1188 g_assert (val <= 0xffffff);
1190 ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_PC, (val & 0xFF0000) >> 16, 16);
1192 ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_PC, 0, 0);
1193 ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_IP, (val & 0xFF00) >> 8, 24);
1194 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, val & 0xFF);
1198 g_assert_not_reached ();
1201 g_assert_not_reached ();
1206 resolve_relocations (MonoImageWriter *acfg)
1210 gsize end_val, start_val;
1215 rr = g_new0 (ElfReloc, acfg->num_relocs);
1218 for (reloc = acfg->relocations; reloc; reloc = reloc->next) {
1219 resolve_reloc (acfg, reloc, &data, &vaddr, &start_val, &end_val);
1220 /* FIXME: little endian */
1221 if (reloc->reloc_type) {
1222 /* Must be static */
1223 g_assert (start_val > 0);
1224 do_reloc (acfg, reloc, data, end_val);
1227 data [1] = end_val >> 8;
1228 data [2] = end_val >> 16;
1229 data [3] = end_val >> 24;
1232 if (start_val == 0 && reloc->val1 [0] != '.') {
1233 rr [i].r_offset = vaddr;
1234 rr [i].r_info = R_386_RELATIVE;
1236 g_assert (i <= acfg->num_relocs);
1242 #endif /* USE_ELF_RELA */
1244 static int normal_sections [] = { SECT_DATA, SECT_DEBUG_FRAME, SECT_DEBUG_INFO, SECT_DEBUG_ABBREV, SECT_DEBUG_LINE, SECT_DEBUG_LOC };
1247 bin_writer_emit_writeout (MonoImageWriter *acfg)
1250 ElfProgHeader progh [4];
1251 ElfSectHeader secth [SECT_NUM];
1257 ElfStrTable str_table = {NULL, NULL};
1258 ElfStrTable sh_str_table = {NULL, NULL};
1259 ElfStrTable dyn_str_table = {NULL, NULL};
1260 BinSection* all_sections [32];
1261 BinSection* sections [SECT_NUM];
1264 ElfDynamic dynamic [14];
1266 int i, num_sections, file_offset, virt_offset, size;
1269 /* Section headers */
1270 memset (§h, 0, sizeof (secth));
1271 memset (&dynamic, 0, sizeof (dynamic));
1272 memset (&header, 0, sizeof (header));
1274 for (i = 1; i < SECT_NUM; ++i) {
1275 secth [i].sh_name = str_table_add (&sh_str_table, section_info [i].name);
1276 secth [i].sh_type = section_info [i].type;
1277 secth [i].sh_addralign = section_info [i].align;
1278 secth [i].sh_flags = section_info [i].flags;
1279 secth [i].sh_entsize = section_info [i].esize;
1281 secth [SECT_DYNSYM].sh_info = SIZEOF_VOID_P == 4 ? 4 : 2;
1282 secth [SECT_SYMTAB].sh_info = SIZEOF_VOID_P == 4 ? 20 : 17;
1283 secth [SECT_HASH].sh_link = SECT_DYNSYM;
1284 secth [SECT_DYNSYM].sh_link = SECT_DYNSTR;
1285 secth [SECT_REL_DYN].sh_link = SECT_DYNSYM;
1286 secth [SECT_RELA_DYN].sh_link = SECT_DYNSYM;
1287 secth [SECT_DYNAMIC].sh_link = SECT_DYNSTR;
1288 secth [SECT_SYMTAB].sh_link = SECT_STRTAB;
1290 num_sections = collect_sections (acfg, secth, all_sections, 16);
1291 hash = build_hash (acfg, num_sections, &dyn_str_table);
1293 g_print ("num_sections: %d\n", num_sections);
1294 g_print ("dynsym: %d, dynstr size: %d\n", hash [1], (int)dyn_str_table.data->len);
1295 for (i = 0; i < num_sections; ++i) {
1296 g_print ("section %s, size: %d, %x\n", all_sections [i]->name, all_sections [i]->cur_offset, all_sections [i]->cur_offset);
1299 /* Associate the bin sections with the ELF sections */
1300 memset (sections, 0, sizeof (sections));
1301 for (i = 0; i < num_sections; ++i) {
1302 BinSection *sect = all_sections [i];
1305 for (j = 0; j < SECT_NUM; ++j) {
1306 if (strcmp (sect->name, section_info [j].name) == 0) {
1312 sections [all_sections [i]->shidx] = sect;
1315 /* at this point we know where in the file the first segment sections go */
1316 dynsym = collect_syms (acfg, hash, &dyn_str_table, NULL, NULL);
1317 num_local_syms = hash [1];
1318 symtab = collect_syms (acfg, NULL, &str_table, secth, &num_local_syms);
1320 file_offset = virt_offset = sizeof (header) + sizeof (progh);
1321 secth [SECT_HASH].sh_addr = secth [SECT_HASH].sh_offset = file_offset;
1322 size = sizeof (int) * (2 + hash [0] + hash [1]);
1323 virt_offset = (file_offset += size);
1324 secth [SECT_HASH].sh_size = size;
1325 secth [SECT_DYNSYM].sh_addr = secth [SECT_DYNSYM].sh_offset = file_offset;
1326 size = sizeof (ElfSymbol) * hash [1];
1327 virt_offset = (file_offset += size);
1328 secth [SECT_DYNSYM].sh_size = size;
1329 secth [SECT_DYNSTR].sh_addr = secth [SECT_DYNSTR].sh_offset = file_offset;
1330 size = dyn_str_table.data->len;
1331 virt_offset = (file_offset += size);
1332 secth [SECT_DYNSTR].sh_size = size;
1334 file_offset &= ~(4-1);
1335 secth [SECT_REL_DYN].sh_addr = secth [SECT_REL_DYN].sh_offset = file_offset;
1336 #ifndef USE_ELF_RELA
1337 size = sizeof (ElfReloc) * acfg->num_relocs;
1341 virt_offset = (file_offset += size);
1342 secth [SECT_REL_DYN].sh_size = size;
1343 secth [SECT_RELA_DYN].sh_addr = secth [SECT_RELA_DYN].sh_offset = file_offset;
1345 size = sizeof (ElfRelocA) * acfg->num_relocs;
1349 virt_offset = (file_offset += size);
1350 secth [SECT_RELA_DYN].sh_size = size;
1352 file_offset = ALIGN_TO (file_offset, secth [SECT_TEXT].sh_addralign);
1353 virt_offset = file_offset;
1354 secth [SECT_TEXT].sh_addr = secth [SECT_TEXT].sh_offset = file_offset;
1355 if (sections [SECT_TEXT]) {
1356 if (sections [SECT_TEXT]->has_addr) {
1357 secth [SECT_TEXT].sh_addr = sections [SECT_TEXT]->addr;
1358 secth [SECT_TEXT].sh_flags &= ~SHF_ALLOC;
1360 size = sections [SECT_TEXT]->cur_offset;
1361 secth [SECT_TEXT].sh_size = size;
1362 file_offset += size;
1365 file_offset = ALIGN_TO (file_offset, secth [SECT_RODATA].sh_addralign);
1366 virt_offset = file_offset;
1367 secth [SECT_RODATA].sh_addr = virt_offset;
1368 secth [SECT_RODATA].sh_offset = file_offset;
1369 if (sections [SECT_RODATA]) {
1370 size = sections [SECT_RODATA]->cur_offset;
1371 secth [SECT_RODATA].sh_size = size;
1372 file_offset += size;
1373 virt_offset += size;
1376 file_offset = ALIGN_TO (file_offset, secth [SECT_DYNAMIC].sh_addralign);
1377 virt_offset = file_offset;
1379 /* .dynamic, .got.plt, .data, .bss here */
1380 /* Have to increase the virt offset since these go to a separate segment */
1381 virt_offset += PAGESIZE;
1382 secth [SECT_DYNAMIC].sh_addr = virt_offset;
1383 secth [SECT_DYNAMIC].sh_offset = file_offset;
1384 size = sizeof (dynamic);
1385 secth [SECT_DYNAMIC].sh_size = size;
1386 file_offset += size;
1387 virt_offset += size;
1389 file_offset = ALIGN_TO (file_offset, secth [SECT_GOT_PLT].sh_addralign);
1390 virt_offset = ALIGN_TO (virt_offset, secth [SECT_GOT_PLT].sh_addralign);
1391 secth [SECT_GOT_PLT].sh_addr = virt_offset;
1392 secth [SECT_GOT_PLT].sh_offset = file_offset;
1393 size = 3 * SIZEOF_VOID_P;
1394 secth [SECT_GOT_PLT].sh_size = size;
1395 file_offset += size;
1396 virt_offset += size;
1398 file_offset = ALIGN_TO (file_offset, secth [SECT_DATA].sh_addralign);
1399 virt_offset = ALIGN_TO (virt_offset, secth [SECT_DATA].sh_addralign);
1400 secth [SECT_DATA].sh_addr = virt_offset;
1401 secth [SECT_DATA].sh_offset = file_offset;
1402 if (sections [SECT_DATA]) {
1403 size = sections [SECT_DATA]->cur_offset;
1404 secth [SECT_DATA].sh_size = size;
1405 file_offset += size;
1406 virt_offset += size;
1409 file_offset = ALIGN_TO (file_offset, secth [SECT_BSS].sh_addralign);
1410 virt_offset = ALIGN_TO (virt_offset, secth [SECT_BSS].sh_addralign);
1411 secth [SECT_BSS].sh_addr = virt_offset;
1412 secth [SECT_BSS].sh_offset = file_offset;
1413 if (sections [SECT_BSS]) {
1414 size = sections [SECT_BSS]->cur_offset;
1415 secth [SECT_BSS].sh_size = size;
1418 /* virtual doesn't matter anymore */
1419 file_offset = ALIGN_TO (file_offset, secth [SECT_DEBUG_FRAME].sh_addralign);
1420 secth [SECT_DEBUG_FRAME].sh_offset = file_offset;
1421 if (sections [SECT_DEBUG_FRAME])
1422 size = sections [SECT_DEBUG_FRAME]->cur_offset;
1425 secth [SECT_DEBUG_FRAME].sh_size = size;
1426 file_offset += size;
1428 secth [SECT_DEBUG_INFO].sh_offset = file_offset;
1429 if (sections [SECT_DEBUG_INFO])
1430 size = sections [SECT_DEBUG_INFO]->cur_offset;
1433 secth [SECT_DEBUG_INFO].sh_size = size;
1434 file_offset += size;
1436 secth [SECT_DEBUG_ABBREV].sh_offset = file_offset;
1437 if (sections [SECT_DEBUG_ABBREV])
1438 size = sections [SECT_DEBUG_ABBREV]->cur_offset;
1441 secth [SECT_DEBUG_ABBREV].sh_size = size;
1442 file_offset += size;
1444 secth [SECT_DEBUG_LINE].sh_offset = file_offset;
1445 if (sections [SECT_DEBUG_LINE])
1446 size = sections [SECT_DEBUG_LINE]->cur_offset;
1449 secth [SECT_DEBUG_LINE].sh_size = size;
1450 file_offset += size;
1452 secth [SECT_DEBUG_LOC].sh_offset = file_offset;
1453 if (sections [SECT_DEBUG_LOC])
1454 size = sections [SECT_DEBUG_LOC]->cur_offset;
1457 secth [SECT_DEBUG_LOC].sh_size = size;
1458 file_offset += size;
1460 file_offset = ALIGN_TO (file_offset, secth [SECT_SHSTRTAB].sh_addralign);
1461 secth [SECT_SHSTRTAB].sh_offset = file_offset;
1462 size = sh_str_table.data->len;
1463 secth [SECT_SHSTRTAB].sh_size = size;
1464 file_offset += size;
1466 file_offset = ALIGN_TO (file_offset, secth [SECT_SYMTAB].sh_addralign);
1467 secth [SECT_SYMTAB].sh_offset = file_offset;
1468 size = sizeof (ElfSymbol) * num_local_syms;
1469 secth [SECT_SYMTAB].sh_size = size;
1470 file_offset += size;
1472 file_offset = ALIGN_TO (file_offset, secth [SECT_STRTAB].sh_addralign);
1473 secth [SECT_STRTAB].sh_offset = file_offset;
1474 size = str_table.data->len;
1475 secth [SECT_STRTAB].sh_size = size;
1476 file_offset += size;
1478 for (i = 1; i < SECT_NUM; ++i) {
1479 if (section_info [i].esize != 0)
1480 g_assert (secth [i].sh_size % section_info [i].esize == 0);
1484 file_offset &= ~(4-1);
1486 header.e_ident [EI_MAG0] = ELFMAG0;
1487 header.e_ident [EI_MAG1] = ELFMAG1;
1488 header.e_ident [EI_MAG2] = ELFMAG2;
1489 header.e_ident [EI_MAG3] = ELFMAG3;
1490 header.e_ident [EI_CLASS] = SIZEOF_VOID_P == 4 ? ELFCLASS32 : ELFCLASS64;
1491 header.e_ident [EI_DATA] = ELFDATA2LSB;
1492 header.e_ident [EI_VERSION] = EV_CURRENT;
1493 header.e_ident [EI_OSABI] = ELFOSABI_NONE;
1494 header.e_ident [EI_ABIVERSION] = 0;
1495 for (i = EI_PAD; i < EI_NIDENT; ++i)
1496 header.e_ident [i] = 0;
1498 header.e_type = ET_DYN;
1499 #if defined(TARGET_X86)
1500 header.e_machine = EM_386;
1501 #elif defined(TARGET_AMD64)
1502 header.e_machine = EM_X86_64;
1503 #elif defined(TARGET_ARM)
1504 header.e_machine = EM_ARM;
1506 g_assert_not_reached ();
1508 header.e_version = 1;
1510 header.e_phoff = sizeof (header);
1511 header.e_ehsize = sizeof (header);
1512 header.e_phentsize = sizeof (ElfProgHeader);
1514 header.e_entry = secth [SECT_TEXT].sh_addr;
1515 header.e_shstrndx = SECT_SHSTRTAB;
1516 header.e_shentsize = sizeof (ElfSectHeader);
1517 header.e_shnum = SECT_NUM;
1518 header.e_shoff = file_offset;
1522 dynamic [i].d_tag = DT_HASH;
1523 dynamic [i].d_un.d_val = secth [SECT_HASH].sh_offset;
1525 dynamic [i].d_tag = DT_STRTAB;
1526 dynamic [i].d_un.d_val = secth [SECT_DYNSTR].sh_offset;
1528 dynamic [i].d_tag = DT_SYMTAB;
1529 dynamic [i].d_un.d_val = secth [SECT_DYNSYM].sh_offset;
1531 dynamic [i].d_tag = DT_STRSZ;
1532 dynamic [i].d_un.d_val = dyn_str_table.data->len;
1534 dynamic [i].d_tag = DT_SYMENT;
1535 dynamic [i].d_un.d_val = sizeof (ElfSymbol);
1538 dynamic [i].d_tag = DT_RELA;
1539 dynamic [i].d_un.d_val = secth [SECT_RELA_DYN].sh_offset;
1541 dynamic [i].d_tag = DT_RELASZ;
1542 dynamic [i].d_un.d_val = secth [SECT_RELA_DYN].sh_size;
1544 dynamic [i].d_tag = DT_RELAENT;
1545 dynamic [i].d_un.d_val = sizeof (ElfRelocA);
1548 dynamic [i].d_tag = DT_REL;
1549 dynamic [i].d_un.d_val = secth [SECT_REL_DYN].sh_offset;
1551 dynamic [i].d_tag = DT_RELSZ;
1552 dynamic [i].d_un.d_val = secth [SECT_REL_DYN].sh_size;
1554 dynamic [i].d_tag = DT_RELENT;
1555 dynamic [i].d_un.d_val = sizeof (ElfReloc);
1558 dynamic [i].d_tag = DT_RELCOUNT;
1559 dynamic [i].d_un.d_val = acfg->num_relocs;
1562 /* Program header */
1563 memset (&progh, 0, sizeof (progh));
1564 progh [0].p_type = PT_LOAD;
1565 progh [0].p_filesz = progh [0].p_memsz = secth [SECT_DYNAMIC].sh_offset;
1566 progh [0].p_align = 4096;
1567 progh [0].p_flags = 5;
1569 progh [1].p_type = PT_LOAD;
1570 progh [1].p_offset = secth [SECT_DYNAMIC].sh_offset;
1571 progh [1].p_vaddr = progh [1].p_paddr = secth [SECT_DYNAMIC].sh_addr;
1572 progh [1].p_filesz = secth [SECT_BSS].sh_offset - secth [SECT_DYNAMIC].sh_offset;
1573 progh [1].p_memsz = secth [SECT_BSS].sh_addr + secth [SECT_BSS].sh_size - secth [SECT_DYNAMIC].sh_addr;
1574 progh [1].p_align = 4096;
1575 progh [1].p_flags = 6;
1577 progh [2].p_type = PT_DYNAMIC;
1578 progh [2].p_offset = secth [SECT_DYNAMIC].sh_offset;
1579 progh [2].p_vaddr = progh [2].p_paddr = secth [SECT_DYNAMIC].sh_addr;
1580 progh [2].p_filesz = progh [2].p_memsz = secth [SECT_DYNAMIC].sh_size;
1581 progh [2].p_align = SIZEOF_VOID_P;
1582 progh [2].p_flags = 6;
1584 progh [3].p_type = PT_GNU_STACK;
1585 progh [3].p_offset = secth [SECT_DYNAMIC].sh_offset;
1586 progh [3].p_vaddr = progh [3].p_paddr = secth [SECT_DYNAMIC].sh_addr;
1587 progh [3].p_filesz = progh [3].p_memsz = secth [SECT_DYNAMIC].sh_size;
1588 progh [3].p_align = SIZEOF_VOID_P;
1589 progh [3].p_flags = 6;
1591 /* Compute the addresses of the bin sections, so relocation can be done */
1592 for (i = 0; i < SECT_NUM; ++i) {
1594 sections [i]->file_offset = secth [i].sh_offset;
1595 sections [i]->virt_offset = secth [i].sh_addr;
1599 reloc_symbols (acfg, dynsym, secth, &dyn_str_table, TRUE);
1600 reloc_symbols (acfg, symtab, secth, &str_table, FALSE);
1601 relocs = resolve_relocations (acfg);
1604 acfg->out_buf_size = file_offset + sizeof (secth);
1605 acfg->out_buf = (guint8 *)g_malloc (acfg->out_buf_size);
1608 bin_writer_fwrite (acfg, &header, sizeof (header), 1);
1609 bin_writer_fwrite (acfg, &progh, sizeof (progh), 1);
1610 bin_writer_fwrite (acfg, hash, sizeof (int) * (hash [0] + hash [1] + 2), 1);
1611 bin_writer_fwrite (acfg, dynsym, sizeof (ElfSymbol) * hash [1], 1);
1612 bin_writer_fwrite (acfg, dyn_str_table.data->str, dyn_str_table.data->len, 1);
1614 bin_writer_fseek (acfg, secth [SECT_REL_DYN].sh_offset);
1615 bin_writer_fwrite (acfg, relocs, sizeof (ElfReloc), acfg->num_relocs);
1618 bin_writer_fseek (acfg, secth [SECT_RELA_DYN].sh_offset);
1619 bin_writer_fwrite (acfg, relocs, secth [SECT_RELA_DYN].sh_size, 1);
1622 if (sections [SECT_TEXT]) {
1623 bin_writer_fseek (acfg, secth [SECT_TEXT].sh_offset);
1624 bin_writer_fwrite (acfg, sections [SECT_TEXT]->data, sections [SECT_TEXT]->cur_offset, 1);
1627 if (sections [SECT_RODATA]) {
1628 bin_writer_fseek (acfg, secth [SECT_RODATA].sh_offset);
1629 bin_writer_fwrite (acfg, sections [SECT_RODATA]->data, sections [SECT_RODATA]->cur_offset, 1);
1632 bin_writer_fseek (acfg, secth [SECT_DYNAMIC].sh_offset);
1633 bin_writer_fwrite (acfg, dynamic, sizeof (dynamic), 1);
1636 size = secth [SECT_DYNAMIC].sh_addr;
1637 bin_writer_fseek (acfg, secth [SECT_GOT_PLT].sh_offset);
1638 bin_writer_fwrite (acfg, &size, sizeof (size), 1);
1640 /* normal sections */
1641 for (i = 0; i < sizeof (normal_sections) / sizeof (normal_sections [0]); ++i) {
1642 int sect = normal_sections [i];
1644 if (sections [sect]) {
1645 bin_writer_fseek (acfg, secth [sect].sh_offset);
1646 bin_writer_fwrite (acfg, sections [sect]->data, sections [sect]->cur_offset, 1);
1650 bin_writer_fseek (acfg, secth [SECT_SHSTRTAB].sh_offset);
1651 bin_writer_fwrite (acfg, sh_str_table.data->str, sh_str_table.data->len, 1);
1652 bin_writer_fseek (acfg, secth [SECT_SYMTAB].sh_offset);
1653 bin_writer_fwrite (acfg, symtab, sizeof (ElfSymbol) * num_local_syms, 1);
1654 bin_writer_fseek (acfg, secth [SECT_STRTAB].sh_offset);
1655 bin_writer_fwrite (acfg, str_table.data->str, str_table.data->len, 1);
1656 /*g_print ("file_offset %d vs %d\n", file_offset, ftell (file));*/
1657 /*g_assert (file_offset >= ftell (file));*/
1658 bin_writer_fseek (acfg, file_offset);
1659 bin_writer_fwrite (acfg, §h, sizeof (secth), 1);
1667 #endif /* USE_ELF_WRITER */
1669 #endif /* USE_BIN_WRITER */
1674 asm_writer_emit_start (MonoImageWriter *acfg)
1676 #if defined(TARGET_ASM_APPLE)
1677 fprintf (acfg->fp, ".subsections_via_symbols\n");
1682 asm_writer_emit_writeout (MonoImageWriter *acfg)
1690 asm_writer_emit_unset_mode (MonoImageWriter *acfg)
1692 if (acfg->mode == EMIT_NONE)
1694 fprintf (acfg->fp, "\n");
1695 acfg->mode = EMIT_NONE;
1699 asm_writer_emit_section_change (MonoImageWriter *acfg, const char *section_name, int subsection_index)
1701 asm_writer_emit_unset_mode (acfg);
1702 #if defined(TARGET_ASM_APPLE)
1703 if (strcmp(section_name, ".bss") == 0)
1704 fprintf (acfg->fp, "%s\n", ".data");
1705 else if (strstr (section_name, ".debug") == section_name) {
1706 //g_assert (subsection_index == 0);
1707 fprintf (acfg->fp, ".section __DWARF, __%s,regular,debug\n", section_name + 1);
1709 fprintf (acfg->fp, "%s\n", section_name);
1710 #elif defined(TARGET_ARM) || defined(TARGET_ARM64) || defined(TARGET_POWERPC)
1711 /* ARM gas doesn't seem to like subsections of .bss */
1712 if (!strcmp (section_name, ".text") || !strcmp (section_name, ".data")) {
1713 fprintf (acfg->fp, "%s %d\n", section_name, subsection_index);
1715 fprintf (acfg->fp, ".section \"%s\"\n", section_name);
1716 fprintf (acfg->fp, ".subsection %d\n", subsection_index);
1718 #elif defined(HOST_WIN32)
1719 fprintf (acfg->fp, ".section %s\n", section_name);
1721 if (!strcmp (section_name, ".text") || !strcmp (section_name, ".data") || !strcmp (section_name, ".bss")) {
1722 fprintf (acfg->fp, "%s %d\n", section_name, subsection_index);
1724 fprintf (acfg->fp, ".section \"%s\"\n", section_name);
1725 fprintf (acfg->fp, ".subsection %d\n", subsection_index);
1731 const char *get_label (const char *s)
1733 #ifdef TARGET_ASM_APPLE
1734 if (s [0] == '.' && s [1] == 'L')
1735 /* apple uses "L" instead of ".L" to mark temporary labels */
1742 #define GLOBAL_SYMBOL_DEF_SCL 2
1743 #define LOCAL_SYMBOL_DEF_SCL 3
1746 asm_writer_in_data_section (MonoImageWriter *acfg)
1748 gboolean in_data_section = FALSE;
1749 const char *data_sections [] = {".data", ".bss", ".rdata"};
1751 for (guchar i = 0; i < G_N_ELEMENTS (data_sections); ++i) {
1752 if (strcmp (acfg->current_section, data_sections [i]) == 0) {
1753 in_data_section = TRUE;
1758 return in_data_section;
1762 asm_writer_emit_symbol_type (MonoImageWriter *acfg, const char *name, gboolean func, gboolean global)
1764 asm_writer_emit_unset_mode (acfg);
1767 fprintf (acfg->fp, "\t.def %s; .scl %d; .type 32; .endef\n", name, (global == TRUE ? GLOBAL_SYMBOL_DEF_SCL : LOCAL_SYMBOL_DEF_SCL));
1769 if (!asm_writer_in_data_section (acfg))
1770 fprintf (acfg->fp, "\t.data\n");
1779 asm_writer_emit_symbol_type (MonoImageWriter *acfg, const char *name, gboolean func, gboolean global)
1788 asm_writer_emit_unset_mode (acfg);
1790 #if defined(TARGET_ASM_APPLE)
1792 #elif defined(TARGET_ARM)
1793 fprintf (acfg->fp, "\t.type %s,#%s\n", name, stype);
1795 fprintf (acfg->fp, "\t.type %s,@%s\n", name, stype);
1798 #endif /* TARGET_WIN32 */
1801 asm_writer_emit_global (MonoImageWriter *acfg, const char *name, gboolean func)
1803 asm_writer_emit_unset_mode (acfg);
1805 fprintf (acfg->fp, "\t.globl %s\n", name);
1807 asm_writer_emit_symbol_type (acfg, name, func, TRUE);
1811 asm_writer_emit_local_symbol (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean func)
1813 asm_writer_emit_unset_mode (acfg);
1815 #if !defined(TARGET_ASM_APPLE) && !defined(TARGET_WIN32)
1816 fprintf (acfg->fp, "\t.local %s\n", name);
1819 asm_writer_emit_symbol_type (acfg, name, func, FALSE);
1823 asm_writer_emit_symbol_size (MonoImageWriter *acfg, const char *name, const char *end_label)
1825 asm_writer_emit_unset_mode (acfg);
1828 #if !defined(TARGET_ASM_APPLE) && !defined(TARGET_WIN32)
1829 fprintf (acfg->fp, "\t.size %s,%s-%s\n", name, end_label, name);
1834 asm_writer_emit_label (MonoImageWriter *acfg, const char *name)
1836 asm_writer_emit_unset_mode (acfg);
1837 fprintf (acfg->fp, "%s:\n", get_label (name));
1841 asm_writer_emit_string (MonoImageWriter *acfg, const char *value)
1843 asm_writer_emit_unset_mode (acfg);
1844 fprintf (acfg->fp, "\t%s \"%s\"\n", AS_STRING_DIRECTIVE, value);
1848 asm_writer_emit_line (MonoImageWriter *acfg)
1850 asm_writer_emit_unset_mode (acfg);
1851 fprintf (acfg->fp, "\n");
1855 asm_writer_emit_alignment (MonoImageWriter *acfg, int size)
1857 asm_writer_emit_unset_mode (acfg);
1858 #if defined(TARGET_ARM)
1859 fprintf (acfg->fp, "\t.align %d\n", ilog2 (size));
1860 #elif defined(__ppc__) && defined(TARGET_ASM_APPLE)
1861 // the mach-o assembler specifies alignments as powers of 2.
1862 fprintf (acfg->fp, "\t.align %d\t; ilog2\n", ilog2(size));
1863 #elif defined(TARGET_ASM_GAS)
1864 fprintf (acfg->fp, "\t.balign %d\n", size);
1865 #elif defined(TARGET_ASM_APPLE)
1866 fprintf (acfg->fp, "\t.align %d\n", ilog2 (size));
1868 fprintf (acfg->fp, "\t.align %d\n", size);
1872 #ifndef USE_BIN_WRITER
1874 asm_writer_emit_alignment_fill (MonoImageWriter *acfg, int size, int fill)
1876 asm_writer_emit_unset_mode (acfg);
1877 #if defined(TARGET_ASM_APPLE)
1878 fprintf (acfg->fp, "\t.align %d, 0x%0x\n", ilog2 (size), fill);
1880 asm_writer_emit_alignment (acfg, size);
1886 asm_writer_emit_pointer_unaligned (MonoImageWriter *acfg, const char *target)
1888 asm_writer_emit_unset_mode (acfg);
1889 fprintf (acfg->fp, "\t%s %s\n", AS_POINTER_DIRECTIVE, target ? target : "0");
1893 asm_writer_emit_pointer (MonoImageWriter *acfg, const char *target)
1895 asm_writer_emit_unset_mode (acfg);
1896 asm_writer_emit_alignment (acfg, sizeof (gpointer));
1897 asm_writer_emit_pointer_unaligned (acfg, target);
1900 static char *byte_to_str;
1903 asm_writer_emit_bytes (MonoImageWriter *acfg, const guint8* buf, int size)
1906 if (acfg->mode != EMIT_BYTE) {
1907 acfg->mode = EMIT_BYTE;
1908 acfg->col_count = 0;
1911 if (byte_to_str == NULL) {
1912 byte_to_str = g_new0 (char, 256 * 8);
1913 for (i = 0; i < 256; ++i) {
1914 sprintf (byte_to_str + (i * 8), ",%d", i);
1918 for (i = 0; i < size; ++i, ++acfg->col_count) {
1919 if ((acfg->col_count % 32) == 0)
1920 fprintf (acfg->fp, "\n\t.byte %d", buf [i]);
1922 fputs (byte_to_str + (buf [i] * 8), acfg->fp);
1927 asm_writer_emit_int16 (MonoImageWriter *acfg, int value)
1929 if (acfg->mode != EMIT_WORD) {
1930 acfg->mode = EMIT_WORD;
1931 acfg->col_count = 0;
1933 if ((acfg->col_count++ % 8) == 0)
1934 fprintf (acfg->fp, "\n\t%s ", AS_INT16_DIRECTIVE);
1936 fprintf (acfg->fp, ", ");
1937 fprintf (acfg->fp, "%d", value);
1941 asm_writer_emit_int32 (MonoImageWriter *acfg, int value)
1943 if (acfg->mode != EMIT_LONG) {
1944 acfg->mode = EMIT_LONG;
1945 acfg->col_count = 0;
1947 if ((acfg->col_count++ % 8) == 0)
1948 fprintf (acfg->fp, "\n\t%s ", AS_INT32_DIRECTIVE);
1950 fprintf (acfg->fp, ",");
1951 fprintf (acfg->fp, "%d", value);
1955 asm_writer_emit_symbol_diff (MonoImageWriter *acfg, const char *end, const char* start, int offset)
1957 #ifdef TARGET_ASM_APPLE
1958 //char symbol [128];
1961 if (acfg->mode != EMIT_LONG) {
1962 acfg->mode = EMIT_LONG;
1963 acfg->col_count = 0;
1966 // FIXME: This doesn't seem to work on the iphone
1968 //#ifdef TARGET_ASM_APPLE
1969 /* The apple assembler needs a separate symbol to be able to handle complex expressions */
1970 sprintf (symbol, "LTMP_SYM%d", acfg->label_gen);
1971 start = get_label (start);
1972 end = get_label (end);
1975 fprintf (acfg->fp, "\n%s=%s - %s + %d", symbol, end, start, offset);
1976 else if (offset < 0)
1977 fprintf (acfg->fp, "\n%s=%s - %s %d", symbol, end, start, offset);
1979 fprintf (acfg->fp, "\n%s=%s - %s", symbol, end, start);
1981 fprintf (acfg->fp, "\n\t%s ", AS_INT32_DIRECTIVE);
1982 fprintf (acfg->fp, "%s", symbol);
1984 start = get_label (start);
1985 end = get_label (end);
1987 if (offset == 0 && strcmp (start, ".") != 0) {
1989 sprintf (symbol, "%sDIFF_SYM%d", AS_TEMP_LABEL_PREFIX, acfg->label_gen);
1991 fprintf (acfg->fp, "\n%s=%s - %s", symbol, end, start);
1992 fprintf (acfg->fp, "\n\t%s ", AS_INT32_DIRECTIVE);
1993 fprintf (acfg->fp, "%s", symbol);
1997 if ((acfg->col_count++ % 8) == 0)
1998 fprintf (acfg->fp, "\n\t%s ", AS_INT32_DIRECTIVE);
2000 fprintf (acfg->fp, ",");
2002 fprintf (acfg->fp, "%s - %s + %d", end, start, offset);
2003 else if (offset < 0)
2004 fprintf (acfg->fp, "%s - %s %d", end, start, offset);
2006 fprintf (acfg->fp, "%s - %s", end, start);
2011 asm_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
2013 asm_writer_emit_unset_mode (acfg);
2014 fprintf (acfg->fp, "\t%s %d\n", AS_SKIP_DIRECTIVE, num);
2017 /* EMIT FUNCTIONS */
2020 mono_img_writer_emit_start (MonoImageWriter *acfg)
2022 #ifdef USE_BIN_WRITER
2023 if (acfg->use_bin_writer)
2024 bin_writer_emit_start (acfg);
2026 asm_writer_emit_start (acfg);
2028 asm_writer_emit_start (acfg);
2033 mono_img_writer_emit_section_change (MonoImageWriter *acfg, const char *section_name, int subsection_index)
2035 #ifdef USE_BIN_WRITER
2036 if (acfg->use_bin_writer)
2037 bin_writer_emit_section_change (acfg, section_name, subsection_index);
2039 asm_writer_emit_section_change (acfg, section_name, subsection_index);
2041 asm_writer_emit_section_change (acfg, section_name, subsection_index);
2044 acfg->current_section = section_name;
2045 acfg->current_subsection = subsection_index;
2049 mono_img_writer_emit_push_section (MonoImageWriter *acfg, const char *section_name, int subsection)
2051 g_assert (acfg->stack_pos < 16 - 1);
2052 acfg->section_stack [acfg->stack_pos] = acfg->current_section;
2053 acfg->subsection_stack [acfg->stack_pos] = acfg->current_subsection;
2056 mono_img_writer_emit_section_change (acfg, section_name, subsection);
2060 mono_img_writer_emit_pop_section (MonoImageWriter *acfg)
2062 g_assert (acfg->stack_pos > 0);
2064 mono_img_writer_emit_section_change (acfg, acfg->section_stack [acfg->stack_pos], acfg->subsection_stack [acfg->stack_pos]);
2068 mono_img_writer_set_section_addr (MonoImageWriter *acfg, guint64 addr)
2070 #ifdef USE_BIN_WRITER
2071 if (!acfg->use_bin_writer)
2074 bin_writer_set_section_addr (acfg, addr);
2081 mono_img_writer_emit_global (MonoImageWriter *acfg, const char *name, gboolean func)
2083 #ifdef USE_BIN_WRITER
2084 if (acfg->use_bin_writer)
2085 bin_writer_emit_global (acfg, name, func);
2087 asm_writer_emit_global (acfg, name, func);
2089 asm_writer_emit_global (acfg, name, func);
2094 mono_img_writer_emit_local_symbol (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean func)
2096 #ifdef USE_BIN_WRITER
2097 if (acfg->use_bin_writer)
2098 bin_writer_emit_local_symbol (acfg, name, end_label, func);
2100 asm_writer_emit_local_symbol (acfg, name, end_label, func);
2102 asm_writer_emit_local_symbol (acfg, name, end_label, func);
2107 mono_img_writer_emit_symbol_size (MonoImageWriter *acfg, const char *name, const char *end_label)
2109 if (!acfg->use_bin_writer)
2110 asm_writer_emit_symbol_size (acfg, name, end_label);
2114 mono_img_writer_emit_label (MonoImageWriter *acfg, const char *name)
2116 #ifdef USE_BIN_WRITER
2117 if (acfg->use_bin_writer)
2118 bin_writer_emit_label (acfg, name);
2120 asm_writer_emit_label (acfg, name);
2122 asm_writer_emit_label (acfg, name);
2127 mono_img_writer_emit_bytes (MonoImageWriter *acfg, const guint8* buf, int size)
2129 #ifdef USE_BIN_WRITER
2130 if (acfg->use_bin_writer)
2131 bin_writer_emit_bytes (acfg, buf, size);
2133 asm_writer_emit_bytes (acfg, buf, size);
2135 asm_writer_emit_bytes (acfg, buf, size);
2140 mono_img_writer_emit_string (MonoImageWriter *acfg, const char *value)
2142 #ifdef USE_BIN_WRITER
2143 if (acfg->use_bin_writer)
2144 bin_writer_emit_string (acfg, value);
2146 asm_writer_emit_string (acfg, value);
2148 asm_writer_emit_string (acfg, value);
2153 mono_img_writer_emit_line (MonoImageWriter *acfg)
2155 #ifdef USE_BIN_WRITER
2156 if (acfg->use_bin_writer)
2157 bin_writer_emit_line (acfg);
2159 asm_writer_emit_line (acfg);
2161 asm_writer_emit_line (acfg);
2166 mono_img_writer_emit_alignment (MonoImageWriter *acfg, int size)
2168 #ifdef USE_BIN_WRITER
2169 if (acfg->use_bin_writer)
2170 bin_writer_emit_alignment (acfg, size);
2172 asm_writer_emit_alignment (acfg, size);
2174 asm_writer_emit_alignment (acfg, size);
2179 mono_img_writer_emit_alignment_fill (MonoImageWriter *acfg, int size, int fill)
2181 #ifdef USE_BIN_WRITER
2182 if (acfg->use_bin_writer)
2183 bin_writer_emit_alignment (acfg, size);
2185 asm_writer_emit_alignment (acfg, size);
2187 asm_writer_emit_alignment_fill (acfg, size, fill);
2192 mono_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 mono_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 mono_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 mono_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 mono_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 mono_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 mono_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 mono_img_writer_emit_byte (MonoImageWriter *acfg, guint8 val)
2285 mono_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 mono_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 * mono_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 mono_img_writer_emit_unset_mode (MonoImageWriter *acfg)
2315 if (!acfg->use_bin_writer)
2316 asm_writer_emit_unset_mode (acfg);
2320 * mono_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 mono_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 mono_bin_writer_supported (void)
2349 #ifdef USE_BIN_WRITER
2357 * mono_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 * mono_img_writer_get_output ().
2364 mono_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 mono_img_writer_destroy (MonoImageWriter *w)
2385 // FIXME: Free all the stuff
2386 mono_mempool_destroy (w->mempool);
2391 mono_img_writer_subsections_supported (MonoImageWriter *acfg)
2393 #ifdef TARGET_ASM_APPLE
2394 return acfg->use_bin_writer;
2401 mono_img_writer_get_fp (MonoImageWriter *acfg)
2407 mono_img_writer_get_temp_label_prefix (MonoImageWriter *acfg)
2409 return AS_TEMP_LABEL_PREFIX;