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(__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(__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"
78 #define AS_POINTER_DIRECTIVE ".long"
81 #if defined(TARGET_ASM_APPLE)
82 #define AS_INT16_DIRECTIVE ".short"
83 #elif defined(TARGET_ASM_GAS)
84 #define AS_INT16_DIRECTIVE ".hword"
86 #define AS_INT16_DIRECTIVE ".word"
89 #if defined(TARGET_ASM_APPLE)
90 #define AS_SKIP_DIRECTIVE ".space"
92 #define AS_SKIP_DIRECTIVE ".skip"
95 #if defined(TARGET_ASM_APPLE)
96 #define AS_GLOBAL_PREFIX "_"
98 #define AS_GLOBAL_PREFIX ""
101 #ifdef TARGET_ASM_APPLE
102 #define AS_TEMP_LABEL_PREFIX "L"
104 #define AS_TEMP_LABEL_PREFIX ".L"
107 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
108 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
109 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
111 #if defined(TARGET_AMD64) && !defined(HOST_WIN32)
112 #define USE_ELF_WRITER 1
113 #define USE_ELF_RELA 1
116 #if defined(TARGET_X86) && !defined(TARGET_WIN32) && !defined(__APPLE__)
117 #define USE_ELF_WRITER 1
120 #if defined(TARGET_ARM) && !defined(__MACH__)
121 #define USE_ELF_WRITER 1
124 #if defined(__mips__)
125 #define USE_ELF_WRITER 1
128 #if defined(USE_ELF_WRITER)
129 #define USE_BIN_WRITER 1
132 #ifdef USE_BIN_WRITER
134 typedef struct _BinSymbol BinSymbol;
135 typedef struct _BinReloc BinReloc;
136 typedef struct _BinSection BinSection;
148 struct _MonoImageWriter {
149 MonoMemPool *mempool;
151 gboolean use_bin_writer;
152 const char *current_section;
153 int current_subsection;
154 const char *section_stack [16];
155 int subsection_stack [16];
159 #ifdef USE_BIN_WRITER
161 BinSection *sections;
162 BinSection *cur_section;
163 BinReloc *relocations;
167 int out_buf_size, out_buf_pos;
171 int mode; /* emit mode */
172 int col_count; /* bytes emitted per .byte line */
176 static G_GNUC_UNUSED int
177 ilog2(register int value)
180 while (value & ~0xf) count += 4, value >>= 4;
181 while (value) count++, value >>= 1;
185 #ifdef USE_BIN_WRITER
187 typedef struct _BinLabel BinLabel;
198 BinSection *val2_section;
211 gboolean is_function;
232 bin_writer_emit_start (MonoImageWriter *acfg)
234 acfg->labels = g_hash_table_new (g_str_hash, g_str_equal);
238 bin_writer_emit_section_change (MonoImageWriter *acfg, const char *section_name, int subsection_index)
242 if (acfg->cur_section && acfg->cur_section->subsection == subsection_index
243 && strcmp (acfg->cur_section->name, section_name) == 0)
245 for (section = acfg->sections; section; section = section->next) {
246 if (section->subsection == subsection_index && strcmp (section->name, section_name) == 0) {
247 acfg->cur_section = section;
252 section = g_new0 (BinSection, 1);
253 section->name = g_strdup (section_name);
254 section->subsection = subsection_index;
255 section->next = acfg->sections;
256 acfg->sections = section;
257 acfg->cur_section = section;
262 bin_writer_set_section_addr (MonoImageWriter *acfg, guint64 addr)
264 acfg->cur_section->addr = addr;
265 acfg->cur_section->has_addr = TRUE;
269 bin_writer_emit_symbol_inner (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean is_global, gboolean func)
271 BinSymbol *symbol = g_new0 (BinSymbol, 1);
272 symbol->name = g_strdup (name);
274 symbol->end_label = g_strdup (end_label);
275 symbol->is_function = func;
276 symbol->is_global = is_global;
277 symbol->section = acfg->cur_section;
278 /* FIXME: we align after this call... */
279 symbol->offset = symbol->section->cur_offset;
280 symbol->next = acfg->symbols;
281 acfg->symbols = symbol;
285 bin_writer_emit_global (MonoImageWriter *acfg, const char *name, gboolean func)
287 bin_writer_emit_symbol_inner (acfg, name, NULL, TRUE, func);
291 bin_writer_emit_local_symbol (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean func)
293 bin_writer_emit_symbol_inner (acfg, name, end_label, FALSE, func);
297 bin_writer_emit_label (MonoImageWriter *acfg, const char *name)
299 BinLabel *label = g_new0 (BinLabel, 1);
300 label->name = g_strdup (name);
301 label->section = acfg->cur_section;
302 label->offset = acfg->cur_section->cur_offset;
303 g_hash_table_insert (acfg->labels, label->name, label);
307 bin_writer_emit_ensure_buffer (BinSection *section, int size)
309 int new_offset = section->cur_offset + size;
310 if (new_offset >= section->data_len) {
311 int new_size = section->data_len? section->data_len * 2: 256;
313 while (new_size <= new_offset)
315 data = g_malloc0 (new_size);
316 #ifdef __native_client_codegen__
317 /* for Native Client, fill empty space with HLT instruction */
319 memset(data, 0xf4, new_size);
321 memcpy (data, section->data, section->data_len);
322 g_free (section->data);
323 section->data = data;
324 section->data_len = new_size;
329 bin_writer_emit_bytes (MonoImageWriter *acfg, const guint8* buf, int size)
331 bin_writer_emit_ensure_buffer (acfg->cur_section, size);
332 memcpy (acfg->cur_section->data + acfg->cur_section->cur_offset, buf, size);
333 acfg->cur_section->cur_offset += size;
337 bin_writer_emit_string (MonoImageWriter *acfg, const char *value)
339 int size = strlen (value) + 1;
340 bin_writer_emit_bytes (acfg, (const guint8*)value, size);
344 bin_writer_emit_line (MonoImageWriter *acfg)
346 /* Nothing to do in binary writer */
350 bin_writer_emit_alignment (MonoImageWriter *acfg, int size)
352 int offset = acfg->cur_section->cur_offset;
354 offset += (size - 1);
355 offset &= ~(size - 1);
356 add = offset - acfg->cur_section->cur_offset;
358 bin_writer_emit_ensure_buffer (acfg->cur_section, add);
359 acfg->cur_section->cur_offset += add;
363 #ifdef __native_client_codegen__
365 bin_writer_emit_nacl_call_alignment (MonoImageWriter *acfg) {
366 int offset = acfg->cur_section->cur_offset;
367 int padding = kNaClAlignment - (offset & kNaClAlignmentMask) - kNaClLengthOfCallImm;
368 guint8 padc = '\x90';
370 if (padding < 0) padding += kNaClAlignment;
372 while (padding > 0) {
373 bin_writer_emit_bytes(acfg, &padc, 1);
377 #endif /* __native_client_codegen__ */
380 bin_writer_emit_pointer_unaligned (MonoImageWriter *acfg, const char *target)
386 g_assert_not_reached ();
387 reloc = g_new0 (BinReloc, 1);
388 reloc->val1 = g_strdup (target);
389 reloc->section = acfg->cur_section;
390 reloc->section_offset = acfg->cur_section->cur_offset;
391 reloc->next = acfg->relocations;
392 acfg->relocations = reloc;
393 if (strcmp (reloc->section->name, ".data") == 0) {
395 //g_print ("reloc: %s at %d\n", target, acfg->cur_section->cur_offset);
397 acfg->cur_section->cur_offset += sizeof (gpointer);
401 bin_writer_emit_pointer (MonoImageWriter *acfg, const char *target)
403 bin_writer_emit_alignment (acfg, sizeof (gpointer));
404 bin_writer_emit_pointer_unaligned (acfg, target);
408 bin_writer_emit_int16 (MonoImageWriter *acfg, int value)
411 bin_writer_emit_ensure_buffer (acfg->cur_section, 2);
412 data = acfg->cur_section->data + acfg->cur_section->cur_offset;
413 acfg->cur_section->cur_offset += 2;
414 /* FIXME: little endian */
416 data [1] = value >> 8;
420 bin_writer_emit_int32 (MonoImageWriter *acfg, int value)
423 bin_writer_emit_ensure_buffer (acfg->cur_section, 4);
424 data = acfg->cur_section->data + acfg->cur_section->cur_offset;
425 acfg->cur_section->cur_offset += 4;
426 /* FIXME: little endian */
428 data [1] = value >> 8;
429 data [2] = value >> 16;
430 data [3] = value >> 24;
434 create_reloc (MonoImageWriter *acfg, const char *end, const char* start, int offset)
437 reloc = mono_mempool_alloc0 (acfg->mempool, sizeof (BinReloc));
438 reloc->val1 = mono_mempool_strdup (acfg->mempool, end);
439 if (strcmp (start, ".") == 0) {
440 reloc->val2_section = acfg->cur_section;
441 reloc->val2_offset = acfg->cur_section->cur_offset;
443 reloc->val2 = mono_mempool_strdup (acfg->mempool, start);
445 reloc->offset = offset;
446 reloc->section = acfg->cur_section;
447 reloc->section_offset = acfg->cur_section->cur_offset;
448 reloc->next = acfg->relocations;
449 acfg->relocations = reloc;
454 bin_writer_emit_symbol_diff (MonoImageWriter *acfg, const char *end, const char* start, int offset)
456 create_reloc (acfg, end, start, offset);
457 acfg->cur_section->cur_offset += 4;
458 /*if (strcmp (reloc->section->name, ".data") == 0) {
460 g_print ("reloc: %s - %s + %d at %d\n", end, start, offset, acfg->cur_section->cur_offset - 4);
465 * Emit a relocation entry of type RELOC_TYPE against symbol SYMBOL at the current PC.
468 static G_GNUC_UNUSED void
469 bin_writer_emit_reloc (MonoImageWriter *acfg, int reloc_type, const char *symbol, int addend)
471 BinReloc *reloc = create_reloc (acfg, symbol, ".", addend);
472 reloc->reloc_type = reloc_type;
476 bin_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
478 bin_writer_emit_ensure_buffer (acfg->cur_section, num);
479 acfg->cur_section->cur_offset += num;
482 #ifdef USE_ELF_WRITER
508 #if SIZEOF_VOID_P == 4
510 typedef Elf32_Ehdr ElfHeader;
511 typedef Elf32_Shdr ElfSectHeader;
512 typedef Elf32_Phdr ElfProgHeader;
513 typedef Elf32_Sym ElfSymbol;
514 typedef Elf32_Rel ElfReloc;
515 typedef Elf32_Rela ElfRelocA;
516 typedef Elf32_Dyn ElfDynamic;
520 typedef Elf64_Ehdr ElfHeader;
521 typedef Elf64_Shdr ElfSectHeader;
522 typedef Elf64_Phdr ElfProgHeader;
523 typedef Elf64_Sym ElfSymbol;
524 typedef Elf64_Rel ElfReloc;
525 typedef Elf64_Rela ElfRelocA;
526 typedef Elf64_Dyn ElfDynamic;
538 static SectInfo section_info [] = {
540 {".hash", SHT_HASH, 4, 2, SIZEOF_VOID_P},
541 {".dynsym", SHT_DYNSYM, sizeof (ElfSymbol), 2, SIZEOF_VOID_P},
542 {".dynstr", SHT_STRTAB, 0, 2, 1},
543 {".rel.dyn", SHT_REL, sizeof (ElfReloc), 2, SIZEOF_VOID_P},
544 {".rela.dyn", SHT_RELA, sizeof (ElfRelocA), 2, SIZEOF_VOID_P},
545 {".text", SHT_PROGBITS, 0, 6, 4096},
546 {".rodata", SHT_PROGBITS, 0, SHF_ALLOC, 4096},
547 {".dynamic", SHT_DYNAMIC, sizeof (ElfDynamic), 3, SIZEOF_VOID_P},
548 {".got.plt", SHT_PROGBITS, SIZEOF_VOID_P, 3, SIZEOF_VOID_P},
549 {".data", SHT_PROGBITS, 0, 3, 8},
550 {".bss", SHT_NOBITS, 0, 3, 8},
551 {".debug_frame", SHT_PROGBITS, 0, 0, 8},
552 {".debug_info", SHT_PROGBITS, 0, 0, 1},
553 {".debug_abbrev", SHT_PROGBITS, 0, 0, 1},
554 {".debug_line", SHT_PROGBITS, 0, 0, 1},
555 {".debug_loc", SHT_PROGBITS, 0, 0, 1},
556 {".shstrtab", SHT_STRTAB, 0, 0, 1},
557 {".symtab", SHT_SYMTAB, sizeof (ElfSymbol), 0, SIZEOF_VOID_P},
558 {".strtab", SHT_STRTAB, 0, 0, 1}
567 str_table_add (ElfStrTable *table, const char* value)
571 table->data = g_string_new_len ("", 1);
572 table->hash = g_hash_table_new (g_str_hash, g_str_equal);
574 idx = GPOINTER_TO_UINT (g_hash_table_lookup (table->hash, value));
577 idx = table->data->len;
578 g_string_append (table->data, value);
579 g_string_append_c (table->data, 0);
580 g_hash_table_insert (table->hash, (void*)value, GUINT_TO_POINTER (idx));
585 append_subsection (MonoImageWriter *acfg, ElfSectHeader *sheaders, BinSection *sect, BinSection *add)
587 int offset = sect->cur_offset;
588 /*offset += (sheaders [sect->shidx].sh_addralign - 1);
589 offset &= ~(sheaders [sect->shidx].sh_addralign - 1);*/
591 * FIXME: we shouldn't align subsections at all, but if we don't then the
592 * stuff inside the subsections which is aligned won't get aligned.
594 if (strcmp (sect->name, ".debug_line") != 0) {
598 bin_writer_emit_ensure_buffer (sect, offset);
599 //g_print ("section %s aligned to %d from %d\n", sect->name, offset, sect->cur_offset);
600 sect->cur_offset = offset;
602 bin_writer_emit_ensure_buffer (sect, add->cur_offset);
603 memcpy (sect->data + sect->cur_offset, add->data, add->cur_offset);
605 sect->cur_offset += add->cur_offset;
606 add->cur_offset = offset; /* it becomes the offset in the parent section */
607 //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);
612 /* merge the subsections */
614 collect_sections (MonoImageWriter *acfg, ElfSectHeader *sheaders, BinSection **out, int num)
616 int i, j, maxs, num_sections;
621 for (sect = acfg->sections; sect; sect = sect->next) {
622 if (sect->subsection == 0) {
623 out [num_sections++] = sect;
624 g_assert (num_sections < num);
626 maxs = MAX (maxs, sect->subsection);
628 for (i = 0; i < num_sections; i++) {
629 for (j = 1; j <= maxs; ++j) {
630 for (sect = acfg->sections; sect; sect = sect->next) {
631 if (sect->subsection == j && strcmp (out [i]->name, sect->name) == 0) {
632 append_subsection (acfg, sheaders, out [i], sect);
641 elf_hash (const unsigned char *name)
643 unsigned long h = 0, g;
645 h = (h << 4) + *name++;
646 if ((g = h & 0xf0000000))
653 #define NUM_BUCKETS 17
656 build_hash (MonoImageWriter *acfg, int num_sections, ElfStrTable *dynstr)
659 int num_symbols = 1 + num_sections + 3;
662 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
663 if (!symbol->is_global)
666 str_table_add (dynstr, symbol->name);
667 /*g_print ("adding sym: %s\n", symbol->name);*/
669 str_table_add (dynstr, "__bss_start");
670 str_table_add (dynstr, "_edata");
671 str_table_add (dynstr, "_end");
673 data = g_new0 (int, num_symbols + 2 + NUM_BUCKETS);
674 data [0] = NUM_BUCKETS;
675 data [1] = num_symbols;
681 get_label_addr (MonoImageWriter *acfg, const char *name)
688 lab = g_hash_table_lookup (acfg->labels, name);
690 g_error ("Undefined label: '%s'.\n", name);
691 section = lab->section;
692 offset = lab->offset;
693 if (section->parent) {
694 value = section->parent->virt_offset + section->cur_offset + offset;
696 value = section->virt_offset + offset;
702 collect_syms (MonoImageWriter *acfg, int *hash, ElfStrTable *strtab, ElfSectHeader *sheaders, int *num_syms)
713 symbols = g_new0 (ElfSymbol, hash [1]);
716 for (symbol = acfg->symbols; symbol; symbol = symbol->next)
719 symbols = g_new0 (ElfSymbol, i + SECT_NUM + 10); /* FIXME */
722 /* the first symbol is undef, all zeroes */
726 for (j = 1; j < SECT_NUM; ++j) {
727 symbols [i].st_info = ELF32_ST_INFO (STB_LOCAL, STT_SECTION);
728 symbols [i].st_shndx = j;
729 symbols [i].st_value = sheaders [j].sh_addr;
733 for (section = acfg->sections; section; section = section->next) {
736 symbols [i].st_info = ELF32_ST_INFO (STB_LOCAL, STT_SECTION);
737 if (strcmp (section->name, ".text") == 0) {
738 symbols [i].st_shndx = SECT_TEXT;
739 section->shidx = SECT_TEXT;
740 section->file_offset = 4096;
741 symbols [i].st_value = section->virt_offset;
742 } else if (strcmp (section->name, ".rodata") == 0) {
743 symbols [i].st_shndx = SECT_RODATA;
744 section->shidx = SECT_RODATA;
745 section->file_offset = 4096;
746 symbols [i].st_value = section->virt_offset;
747 } else if (strcmp (section->name, ".data") == 0) {
748 symbols [i].st_shndx = SECT_DATA;
749 section->shidx = SECT_DATA;
750 section->file_offset = 4096 + 28; /* FIXME */
751 symbols [i].st_value = section->virt_offset;
752 } else if (strcmp (section->name, ".bss") == 0) {
753 symbols [i].st_shndx = SECT_BSS;
754 section->shidx = SECT_BSS;
755 section->file_offset = 4096 + 28 + 8; /* FIXME */
756 symbols [i].st_value = section->virt_offset;
761 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
764 if (!symbol->is_global && hash)
766 symbols [i].st_info = ELF32_ST_INFO (symbol->is_global ? STB_GLOBAL : STB_LOCAL, symbol->is_function? STT_FUNC : STT_OBJECT);
767 symbols [i].st_name = str_table_add (strtab, symbol->name);
768 /*g_print ("sym name %s tabled to %d\n", symbol->name, symbols [i].st_name);*/
769 section = symbol->section;
770 symbols [i].st_shndx = section->parent? section->parent->shidx: section->shidx;
771 lab = g_hash_table_lookup (acfg->labels, symbol->name);
772 offset = lab->offset;
773 if (section->parent) {
774 symbols [i].st_value = section->parent->virt_offset + section->cur_offset + offset;
776 symbols [i].st_value = section->virt_offset + offset;
779 if (symbol->end_label) {
780 BinLabel *elab = g_hash_table_lookup (acfg->labels, symbol->end_label);
782 symbols [i].st_size = elab->offset - lab->offset;
786 /* add special symbols */
787 symbols [i].st_name = str_table_add (strtab, "__bss_start");
788 symbols [i].st_shndx = 0xfff1;
789 symbols [i].st_info = ELF32_ST_INFO (STB_GLOBAL, 0);
791 symbols [i].st_name = str_table_add (strtab, "_edata");
792 symbols [i].st_shndx = 0xfff1;
793 symbols [i].st_info = ELF32_ST_INFO (STB_GLOBAL, 0);
795 symbols [i].st_name = str_table_add (strtab, "_end");
796 symbols [i].st_shndx = 0xfff1;
797 symbols [i].st_info = ELF32_ST_INFO (STB_GLOBAL, 0);
803 /* add to hash table */
806 chain = hash + 2 + hash [0];
807 for (i = 0; i < hash [1]; ++i) {
809 /*g_print ("checking %d '%s' (sym %d)\n", symbols [i].st_name, strtab->data->str + symbols [i].st_name, i);*/
810 if (!symbols [i].st_name)
812 hashc = elf_hash ((guint8*)strtab->data->str + symbols [i].st_name);
813 slot = hashc % hash [0];
814 /*g_print ("hashing '%s' at slot %d (sym %d)\n", strtab->data->str + symbols [i].st_name, slot, i);*/
816 chain [i] = bucket [slot];
827 reloc_symbols (MonoImageWriter *acfg, ElfSymbol *symbols, ElfSectHeader *sheaders, ElfStrTable *strtab, gboolean dynamic)
835 for (section = acfg->sections; section; section = section->next) {
838 symbols [i].st_value = sheaders [section->shidx].sh_addr;
842 for (i = 1; i < SECT_NUM; ++i) {
843 symbols [i].st_value = sheaders [i].sh_addr;
846 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
849 if (dynamic && !symbol->is_global)
851 section = symbol->section;
852 lab = g_hash_table_lookup (acfg->labels, symbol->name);
853 offset = lab->offset;
854 if (section->parent) {
855 symbols [i].st_value = sheaders [section->parent->shidx].sh_addr + section->cur_offset + offset;
857 symbols [i].st_value = sheaders [section->shidx].sh_addr + offset;
862 symbols [i].st_value = sheaders [SECT_BSS].sh_addr;
865 symbols [i].st_value = sheaders [SECT_DATA].sh_addr + sheaders [SECT_DATA].sh_size;
868 symbols [i].st_value = sheaders [SECT_BSS].sh_addr + sheaders [SECT_BSS].sh_size;
873 resolve_reloc (MonoImageWriter *acfg, BinReloc *reloc, guint8 **out_data, gsize *out_vaddr, gsize *out_start_val, gsize *out_end_val)
876 gssize end_val, start_val;
879 end_val = get_label_addr (acfg, reloc->val1);
881 start_val = get_label_addr (acfg, reloc->val2);
882 } else if (reloc->val2_section) {
883 start_val = reloc->val2_offset;
884 if (reloc->val2_section->parent)
885 start_val += reloc->val2_section->parent->virt_offset + reloc->val2_section->cur_offset;
887 start_val += reloc->val2_section->virt_offset;
891 end_val = end_val - start_val + reloc->offset;
892 if (reloc->section->parent) {
893 data = reloc->section->parent->data;
894 data += reloc->section->cur_offset;
895 data += reloc->section_offset;
896 vaddr = reloc->section->parent->virt_offset;
897 vaddr += reloc->section->cur_offset;
898 vaddr += reloc->section_offset;
900 data = reloc->section->data;
901 data += reloc->section_offset;
902 vaddr = reloc->section->virt_offset;
903 vaddr += reloc->section_offset;
906 *out_start_val = start_val;
907 *out_end_val = end_val;
915 resolve_relocations (MonoImageWriter *acfg)
919 gsize end_val, start_val;
924 rr = g_new0 (ElfRelocA, acfg->num_relocs);
927 for (reloc = acfg->relocations; reloc; reloc = reloc->next) {
928 resolve_reloc (acfg, reloc, &data, &vaddr, &start_val, &end_val);
929 /* FIXME: little endian */
931 data [1] = end_val >> 8;
932 data [2] = end_val >> 16;
933 data [3] = end_val >> 24;
935 if (start_val == 0 && reloc->val1 [0] != '.') {
936 rr [i].r_offset = vaddr;
937 rr [i].r_info = R_X86_64_RELATIVE;
938 rr [i].r_addend = end_val;
940 g_assert (i <= acfg->num_relocs);
946 #else /* USE_ELF_RELA */
949 do_reloc (MonoImageWriter *acfg, BinReloc *reloc, guint8 *data, gssize addr)
953 * We use the official ARM relocation types, but implement only the stuff actually
954 * needed by the code we generate.
956 switch (reloc->reloc_type) {
959 guint32 *code = (guint32*)(gpointer)data;
963 if (reloc->reloc_type == R_ARM_CALL)
965 g_assert (data [3] == 0xeb);
968 g_assert (data [3] == 0xea);
969 if (diff >= 0 && diff <= 33554431) {
971 ins = (ins & 0xff000000) | diff;
973 } else if (diff <= 0 && diff >= -33554432) {
975 ins = (ins & 0xff000000) | (diff & ~0xff000000);
978 g_assert_not_reached ();
982 case R_ARM_ALU_PC_G0_NC: {
983 /* Generated by emit_plt () */
987 g_assert (val <= 0xffffff);
989 ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_PC, (val & 0xFF0000) >> 16, 16);
991 ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_PC, 0, 0);
992 ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_IP, (val & 0xFF00) >> 8, 24);
993 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, val & 0xFF);
997 g_assert_not_reached ();
1000 g_assert_not_reached ();
1005 resolve_relocations (MonoImageWriter *acfg)
1009 gsize end_val, start_val;
1014 rr = g_new0 (ElfReloc, acfg->num_relocs);
1017 for (reloc = acfg->relocations; reloc; reloc = reloc->next) {
1018 resolve_reloc (acfg, reloc, &data, &vaddr, &start_val, &end_val);
1019 /* FIXME: little endian */
1020 if (reloc->reloc_type) {
1021 /* Must be static */
1022 g_assert (start_val > 0);
1023 do_reloc (acfg, reloc, data, end_val);
1026 data [1] = end_val >> 8;
1027 data [2] = end_val >> 16;
1028 data [3] = end_val >> 24;
1031 if (start_val == 0 && reloc->val1 [0] != '.') {
1032 rr [i].r_offset = vaddr;
1033 rr [i].r_info = R_386_RELATIVE;
1035 g_assert (i <= acfg->num_relocs);
1041 #endif /* USE_ELF_RELA */
1044 bin_writer_fwrite (MonoImageWriter *acfg, void *val, size_t size, size_t nmemb)
1047 fwrite (val, size, nmemb, acfg->fp);
1049 g_assert (acfg->out_buf_pos + (size * nmemb) <= acfg->out_buf_size);
1050 memcpy (acfg->out_buf + acfg->out_buf_pos, val, size * nmemb);
1051 acfg->out_buf_pos += (size * nmemb);
1056 bin_writer_fseek (MonoImageWriter *acfg, int offset)
1059 fseek (acfg->fp, offset, SEEK_SET);
1061 acfg->out_buf_pos = offset;
1064 static int normal_sections [] = { SECT_DATA, SECT_DEBUG_FRAME, SECT_DEBUG_INFO, SECT_DEBUG_ABBREV, SECT_DEBUG_LINE, SECT_DEBUG_LOC };
1067 bin_writer_emit_writeout (MonoImageWriter *acfg)
1071 ElfProgHeader progh [3];
1072 ElfSectHeader secth [SECT_NUM];
1078 ElfStrTable str_table = {NULL, NULL};
1079 ElfStrTable sh_str_table = {NULL, NULL};
1080 ElfStrTable dyn_str_table = {NULL, NULL};
1081 BinSection* all_sections [32];
1082 BinSection* sections [SECT_NUM];
1085 ElfDynamic dynamic [14];
1087 int i, num_sections, file_offset, virt_offset, size, num_symtab;
1092 /* Section headers */
1093 memset (§h, 0, sizeof (secth));
1094 memset (&dynamic, 0, sizeof (dynamic));
1095 memset (&header, 0, sizeof (header));
1097 for (i = 1; i < SECT_NUM; ++i) {
1098 secth [i].sh_name = str_table_add (&sh_str_table, section_info [i].name);
1099 secth [i].sh_type = section_info [i].type;
1100 secth [i].sh_addralign = section_info [i].align;
1101 secth [i].sh_flags = section_info [i].flags;
1102 secth [i].sh_entsize = section_info [i].esize;
1104 secth [SECT_DYNSYM].sh_info = SIZEOF_VOID_P == 4 ? 4 : 2;
1105 secth [SECT_SYMTAB].sh_info = SIZEOF_VOID_P == 4 ? 20 : 17;
1106 secth [SECT_HASH].sh_link = SECT_DYNSYM;
1107 secth [SECT_DYNSYM].sh_link = SECT_DYNSTR;
1108 secth [SECT_REL_DYN].sh_link = SECT_DYNSYM;
1109 secth [SECT_RELA_DYN].sh_link = SECT_DYNSYM;
1110 secth [SECT_DYNAMIC].sh_link = SECT_DYNSTR;
1111 secth [SECT_SYMTAB].sh_link = SECT_STRTAB;
1113 num_sections = collect_sections (acfg, secth, all_sections, 16);
1114 hash = build_hash (acfg, num_sections, &dyn_str_table);
1115 num_symtab = hash [1]; /* FIXME */
1117 g_print ("num_sections: %d\n", num_sections);
1118 g_print ("dynsym: %d, dynstr size: %d\n", hash [1], (int)dyn_str_table.data->len);
1119 for (i = 0; i < num_sections; ++i) {
1120 g_print ("section %s, size: %d, %x\n", all_sections [i]->name, all_sections [i]->cur_offset, all_sections [i]->cur_offset);
1123 /* Associate the bin sections with the ELF sections */
1124 memset (sections, 0, sizeof (sections));
1125 for (i = 0; i < num_sections; ++i) {
1126 BinSection *sect = all_sections [i];
1129 for (j = 0; j < SECT_NUM; ++j) {
1130 if (strcmp (sect->name, section_info [j].name) == 0) {
1136 sections [all_sections [i]->shidx] = sect;
1139 /* at this point we know where in the file the first segment sections go */
1140 dynsym = collect_syms (acfg, hash, &dyn_str_table, NULL, NULL);
1141 num_local_syms = hash [1];
1142 symtab = collect_syms (acfg, NULL, &str_table, secth, &num_local_syms);
1144 file_offset = virt_offset = sizeof (header) + sizeof (progh);
1145 secth [SECT_HASH].sh_addr = secth [SECT_HASH].sh_offset = file_offset;
1146 size = sizeof (int) * (2 + hash [0] + hash [1]);
1147 virt_offset = (file_offset += size);
1148 secth [SECT_HASH].sh_size = size;
1149 secth [SECT_DYNSYM].sh_addr = secth [SECT_DYNSYM].sh_offset = file_offset;
1150 size = sizeof (ElfSymbol) * hash [1];
1151 virt_offset = (file_offset += size);
1152 secth [SECT_DYNSYM].sh_size = size;
1153 secth [SECT_DYNSTR].sh_addr = secth [SECT_DYNSTR].sh_offset = file_offset;
1154 size = dyn_str_table.data->len;
1155 virt_offset = (file_offset += size);
1156 secth [SECT_DYNSTR].sh_size = size;
1158 file_offset &= ~(4-1);
1159 secth [SECT_REL_DYN].sh_addr = secth [SECT_REL_DYN].sh_offset = file_offset;
1160 #ifndef USE_ELF_RELA
1161 size = sizeof (ElfReloc) * acfg->num_relocs;
1165 virt_offset = (file_offset += size);
1166 secth [SECT_REL_DYN].sh_size = size;
1167 secth [SECT_RELA_DYN].sh_addr = secth [SECT_RELA_DYN].sh_offset = file_offset;
1169 size = sizeof (ElfRelocA) * acfg->num_relocs;
1173 virt_offset = (file_offset += size);
1174 secth [SECT_RELA_DYN].sh_size = size;
1176 file_offset = ALIGN_TO (file_offset, secth [SECT_TEXT].sh_addralign);
1177 virt_offset = file_offset;
1178 secth [SECT_TEXT].sh_addr = secth [SECT_TEXT].sh_offset = file_offset;
1179 if (sections [SECT_TEXT]) {
1180 if (sections [SECT_TEXT]->has_addr) {
1181 secth [SECT_TEXT].sh_addr = sections [SECT_TEXT]->addr;
1182 secth [SECT_TEXT].sh_flags &= ~SHF_ALLOC;
1184 size = sections [SECT_TEXT]->cur_offset;
1185 secth [SECT_TEXT].sh_size = size;
1186 file_offset += size;
1189 file_offset = ALIGN_TO (file_offset, secth [SECT_RODATA].sh_addralign);
1190 virt_offset = file_offset;
1191 secth [SECT_RODATA].sh_addr = virt_offset;
1192 secth [SECT_RODATA].sh_offset = file_offset;
1193 if (sections [SECT_RODATA]) {
1194 size = sections [SECT_RODATA]->cur_offset;
1195 secth [SECT_RODATA].sh_size = size;
1196 file_offset += size;
1197 virt_offset += size;
1200 file_offset = ALIGN_TO (file_offset, secth [SECT_DYNAMIC].sh_addralign);
1201 virt_offset = file_offset;
1203 /* .dynamic, .got.plt, .data, .bss here */
1204 /* Have to increase the virt offset since these go to a separate segment */
1205 virt_offset += PAGESIZE;
1206 secth [SECT_DYNAMIC].sh_addr = virt_offset;
1207 secth [SECT_DYNAMIC].sh_offset = file_offset;
1208 size = sizeof (dynamic);
1209 secth [SECT_DYNAMIC].sh_size = size;
1210 file_offset += size;
1211 virt_offset += size;
1213 file_offset = ALIGN_TO (file_offset, secth [SECT_GOT_PLT].sh_addralign);
1214 virt_offset = ALIGN_TO (virt_offset, secth [SECT_GOT_PLT].sh_addralign);
1215 secth [SECT_GOT_PLT].sh_addr = virt_offset;
1216 secth [SECT_GOT_PLT].sh_offset = file_offset;
1217 size = 3 * SIZEOF_VOID_P;
1218 secth [SECT_GOT_PLT].sh_size = size;
1219 file_offset += size;
1220 virt_offset += size;
1222 file_offset = ALIGN_TO (file_offset, secth [SECT_DATA].sh_addralign);
1223 virt_offset = ALIGN_TO (virt_offset, secth [SECT_DATA].sh_addralign);
1224 secth [SECT_DATA].sh_addr = virt_offset;
1225 secth [SECT_DATA].sh_offset = file_offset;
1226 if (sections [SECT_DATA]) {
1227 size = sections [SECT_DATA]->cur_offset;
1228 secth [SECT_DATA].sh_size = size;
1229 file_offset += size;
1230 virt_offset += size;
1233 file_offset = ALIGN_TO (file_offset, secth [SECT_BSS].sh_addralign);
1234 virt_offset = ALIGN_TO (virt_offset, secth [SECT_BSS].sh_addralign);
1235 secth [SECT_BSS].sh_addr = virt_offset;
1236 secth [SECT_BSS].sh_offset = file_offset;
1237 if (sections [SECT_BSS]) {
1238 size = sections [SECT_BSS]->cur_offset;
1239 secth [SECT_BSS].sh_size = size;
1242 /* virtual doesn't matter anymore */
1243 file_offset = ALIGN_TO (file_offset, secth [SECT_DEBUG_FRAME].sh_addralign);
1244 secth [SECT_DEBUG_FRAME].sh_offset = file_offset;
1245 if (sections [SECT_DEBUG_FRAME])
1246 size = sections [SECT_DEBUG_FRAME]->cur_offset;
1249 secth [SECT_DEBUG_FRAME].sh_size = size;
1250 file_offset += size;
1252 secth [SECT_DEBUG_INFO].sh_offset = file_offset;
1253 if (sections [SECT_DEBUG_INFO])
1254 size = sections [SECT_DEBUG_INFO]->cur_offset;
1257 secth [SECT_DEBUG_INFO].sh_size = size;
1258 file_offset += size;
1260 secth [SECT_DEBUG_ABBREV].sh_offset = file_offset;
1261 if (sections [SECT_DEBUG_ABBREV])
1262 size = sections [SECT_DEBUG_ABBREV]->cur_offset;
1265 secth [SECT_DEBUG_ABBREV].sh_size = size;
1266 file_offset += size;
1268 secth [SECT_DEBUG_LINE].sh_offset = file_offset;
1269 if (sections [SECT_DEBUG_LINE])
1270 size = sections [SECT_DEBUG_LINE]->cur_offset;
1273 secth [SECT_DEBUG_LINE].sh_size = size;
1274 file_offset += size;
1276 secth [SECT_DEBUG_LOC].sh_offset = file_offset;
1277 if (sections [SECT_DEBUG_LOC])
1278 size = sections [SECT_DEBUG_LOC]->cur_offset;
1281 secth [SECT_DEBUG_LOC].sh_size = size;
1282 file_offset += size;
1284 file_offset = ALIGN_TO (file_offset, secth [SECT_SHSTRTAB].sh_addralign);
1285 secth [SECT_SHSTRTAB].sh_offset = file_offset;
1286 size = sh_str_table.data->len;
1287 secth [SECT_SHSTRTAB].sh_size = size;
1288 file_offset += size;
1290 file_offset = ALIGN_TO (file_offset, secth [SECT_SYMTAB].sh_addralign);
1291 secth [SECT_SYMTAB].sh_offset = file_offset;
1292 size = sizeof (ElfSymbol) * num_local_syms;
1293 secth [SECT_SYMTAB].sh_size = size;
1294 file_offset += size;
1296 file_offset = ALIGN_TO (file_offset, secth [SECT_STRTAB].sh_addralign);
1297 secth [SECT_STRTAB].sh_offset = file_offset;
1298 size = str_table.data->len;
1299 secth [SECT_STRTAB].sh_size = size;
1300 file_offset += size;
1302 for (i = 1; i < SECT_NUM; ++i) {
1303 if (section_info [i].esize != 0)
1304 g_assert (secth [i].sh_size % section_info [i].esize == 0);
1308 file_offset &= ~(4-1);
1310 header.e_ident [EI_MAG0] = ELFMAG0;
1311 header.e_ident [EI_MAG1] = ELFMAG1;
1312 header.e_ident [EI_MAG2] = ELFMAG2;
1313 header.e_ident [EI_MAG3] = ELFMAG3;
1314 header.e_ident [EI_CLASS] = SIZEOF_VOID_P == 4 ? ELFCLASS32 : ELFCLASS64;
1315 header.e_ident [EI_DATA] = ELFDATA2LSB;
1316 header.e_ident [EI_VERSION] = EV_CURRENT;
1317 header.e_ident [EI_OSABI] = ELFOSABI_NONE;
1318 header.e_ident [EI_ABIVERSION] = 0;
1319 for (i = EI_PAD; i < EI_NIDENT; ++i)
1320 header.e_ident [i] = 0;
1322 header.e_type = ET_DYN;
1323 #if defined(TARGET_X86)
1324 header.e_machine = EM_386;
1325 #elif defined(TARGET_AMD64)
1326 header.e_machine = EM_X86_64;
1327 #elif defined(TARGET_ARM)
1328 header.e_machine = EM_ARM;
1330 g_assert_not_reached ();
1332 header.e_version = 1;
1334 header.e_phoff = sizeof (header);
1335 header.e_ehsize = sizeof (header);
1336 header.e_phentsize = sizeof (ElfProgHeader);
1338 header.e_entry = secth [SECT_TEXT].sh_addr;
1339 header.e_shstrndx = SECT_SHSTRTAB;
1340 header.e_shentsize = sizeof (ElfSectHeader);
1341 header.e_shnum = SECT_NUM;
1342 header.e_shoff = file_offset;
1346 dynamic [i].d_tag = DT_HASH;
1347 dynamic [i].d_un.d_val = secth [SECT_HASH].sh_offset;
1349 dynamic [i].d_tag = DT_STRTAB;
1350 dynamic [i].d_un.d_val = secth [SECT_DYNSTR].sh_offset;
1352 dynamic [i].d_tag = DT_SYMTAB;
1353 dynamic [i].d_un.d_val = secth [SECT_DYNSYM].sh_offset;
1355 dynamic [i].d_tag = DT_STRSZ;
1356 dynamic [i].d_un.d_val = dyn_str_table.data->len;
1358 dynamic [i].d_tag = DT_SYMENT;
1359 dynamic [i].d_un.d_val = sizeof (ElfSymbol);
1362 dynamic [i].d_tag = DT_RELA;
1363 dynamic [i].d_un.d_val = secth [SECT_RELA_DYN].sh_offset;
1365 dynamic [i].d_tag = DT_RELASZ;
1366 dynamic [i].d_un.d_val = secth [SECT_RELA_DYN].sh_size;
1368 dynamic [i].d_tag = DT_RELAENT;
1369 dynamic [i].d_un.d_val = sizeof (ElfRelocA);
1372 dynamic [i].d_tag = DT_REL;
1373 dynamic [i].d_un.d_val = secth [SECT_REL_DYN].sh_offset;
1375 dynamic [i].d_tag = DT_RELSZ;
1376 dynamic [i].d_un.d_val = secth [SECT_REL_DYN].sh_size;
1378 dynamic [i].d_tag = DT_RELENT;
1379 dynamic [i].d_un.d_val = sizeof (ElfReloc);
1382 dynamic [i].d_tag = DT_RELCOUNT;
1383 dynamic [i].d_un.d_val = acfg->num_relocs;
1386 /* Program header */
1387 memset (&progh, 0, sizeof (progh));
1388 progh [0].p_type = PT_LOAD;
1389 progh [0].p_filesz = progh [0].p_memsz = secth [SECT_DYNAMIC].sh_offset;
1390 progh [0].p_align = 4096;
1391 progh [0].p_flags = 5;
1393 progh [1].p_type = PT_LOAD;
1394 progh [1].p_offset = secth [SECT_DYNAMIC].sh_offset;
1395 progh [1].p_vaddr = progh [1].p_paddr = secth [SECT_DYNAMIC].sh_addr;
1396 progh [1].p_filesz = secth [SECT_BSS].sh_offset - secth [SECT_DYNAMIC].sh_offset;
1397 progh [1].p_memsz = secth [SECT_BSS].sh_addr + secth [SECT_BSS].sh_size - secth [SECT_DYNAMIC].sh_addr;
1398 progh [1].p_align = 4096;
1399 progh [1].p_flags = 6;
1401 progh [2].p_type = PT_DYNAMIC;
1402 progh [2].p_offset = secth [SECT_DYNAMIC].sh_offset;
1403 progh [2].p_vaddr = progh [2].p_paddr = secth [SECT_DYNAMIC].sh_addr;
1404 progh [2].p_filesz = progh [2].p_memsz = secth [SECT_DYNAMIC].sh_size;
1405 progh [2].p_align = SIZEOF_VOID_P;
1406 progh [2].p_flags = 6;
1408 /* Compute the addresses of the bin sections, so relocation can be done */
1409 for (i = 0; i < SECT_NUM; ++i) {
1411 sections [i]->file_offset = secth [i].sh_offset;
1412 sections [i]->virt_offset = secth [i].sh_addr;
1416 reloc_symbols (acfg, dynsym, secth, &dyn_str_table, TRUE);
1417 reloc_symbols (acfg, symtab, secth, &str_table, FALSE);
1418 relocs = resolve_relocations (acfg);
1421 acfg->out_buf_size = file_offset + sizeof (secth);
1422 acfg->out_buf = g_malloc (acfg->out_buf_size);
1425 bin_writer_fwrite (acfg, &header, sizeof (header), 1);
1426 bin_writer_fwrite (acfg, &progh, sizeof (progh), 1);
1427 bin_writer_fwrite (acfg, hash, sizeof (int) * (hash [0] + hash [1] + 2), 1);
1428 bin_writer_fwrite (acfg, dynsym, sizeof (ElfSymbol) * hash [1], 1);
1429 bin_writer_fwrite (acfg, dyn_str_table.data->str, dyn_str_table.data->len, 1);
1431 bin_writer_fseek (acfg, secth [SECT_REL_DYN].sh_offset);
1432 bin_writer_fwrite (acfg, relocs, sizeof (ElfReloc), acfg->num_relocs);
1435 bin_writer_fseek (acfg, secth [SECT_RELA_DYN].sh_offset);
1436 bin_writer_fwrite (acfg, relocs, secth [SECT_RELA_DYN].sh_size, 1);
1439 if (sections [SECT_TEXT]) {
1440 bin_writer_fseek (acfg, secth [SECT_TEXT].sh_offset);
1441 bin_writer_fwrite (acfg, sections [SECT_TEXT]->data, sections [SECT_TEXT]->cur_offset, 1);
1444 if (sections [SECT_RODATA]) {
1445 bin_writer_fseek (acfg, secth [SECT_RODATA].sh_offset);
1446 bin_writer_fwrite (acfg, sections [SECT_RODATA]->data, sections [SECT_RODATA]->cur_offset, 1);
1449 bin_writer_fseek (acfg, secth [SECT_DYNAMIC].sh_offset);
1450 bin_writer_fwrite (acfg, dynamic, sizeof (dynamic), 1);
1453 size = secth [SECT_DYNAMIC].sh_addr;
1454 bin_writer_fseek (acfg, secth [SECT_GOT_PLT].sh_offset);
1455 bin_writer_fwrite (acfg, &size, sizeof (size), 1);
1457 /* normal sections */
1458 for (i = 0; i < sizeof (normal_sections) / sizeof (normal_sections [0]); ++i) {
1459 int sect = normal_sections [i];
1461 if (sections [sect]) {
1462 bin_writer_fseek (acfg, secth [sect].sh_offset);
1463 bin_writer_fwrite (acfg, sections [sect]->data, sections [sect]->cur_offset, 1);
1467 bin_writer_fseek (acfg, secth [SECT_SHSTRTAB].sh_offset);
1468 bin_writer_fwrite (acfg, sh_str_table.data->str, sh_str_table.data->len, 1);
1469 bin_writer_fseek (acfg, secth [SECT_SYMTAB].sh_offset);
1470 bin_writer_fwrite (acfg, symtab, sizeof (ElfSymbol) * num_local_syms, 1);
1471 bin_writer_fseek (acfg, secth [SECT_STRTAB].sh_offset);
1472 bin_writer_fwrite (acfg, str_table.data->str, str_table.data->len, 1);
1473 /*g_print ("file_offset %d vs %d\n", file_offset, ftell (file));*/
1474 /*g_assert (file_offset >= ftell (file));*/
1475 bin_writer_fseek (acfg, file_offset);
1476 bin_writer_fwrite (acfg, §h, sizeof (secth), 1);
1484 #endif /* USE_ELF_WRITER */
1486 #endif /* USE_BIN_WRITER */
1491 asm_writer_emit_start (MonoImageWriter *acfg)
1496 asm_writer_emit_writeout (MonoImageWriter *acfg)
1504 asm_writer_emit_unset_mode (MonoImageWriter *acfg)
1506 if (acfg->mode == EMIT_NONE)
1508 fprintf (acfg->fp, "\n");
1509 acfg->mode = EMIT_NONE;
1513 asm_writer_emit_section_change (MonoImageWriter *acfg, const char *section_name, int subsection_index)
1515 asm_writer_emit_unset_mode (acfg);
1516 #if defined(TARGET_ASM_APPLE)
1517 if (strcmp(section_name, ".bss") == 0)
1518 fprintf (acfg->fp, "%s\n", ".data");
1519 else if (strstr (section_name, ".debug") == section_name) {
1520 //g_assert (subsection_index == 0);
1521 fprintf (acfg->fp, ".section __DWARF, __%s,regular,debug\n", section_name + 1);
1523 fprintf (acfg->fp, "%s\n", section_name);
1524 #elif defined(TARGET_ARM) || defined(TARGET_POWERPC)
1525 /* ARM gas doesn't seem to like subsections of .bss */
1526 if (!strcmp (section_name, ".text") || !strcmp (section_name, ".data")) {
1527 fprintf (acfg->fp, "%s %d\n", section_name, subsection_index);
1529 fprintf (acfg->fp, ".section \"%s\"\n", section_name);
1530 fprintf (acfg->fp, ".subsection %d\n", subsection_index);
1532 #elif defined(HOST_WIN32)
1533 fprintf (acfg->fp, ".section %s\n", section_name);
1535 if (!strcmp (section_name, ".text") || !strcmp (section_name, ".data") || !strcmp (section_name, ".bss")) {
1536 fprintf (acfg->fp, "%s %d\n", section_name, subsection_index);
1538 fprintf (acfg->fp, ".section \"%s\"\n", section_name);
1539 fprintf (acfg->fp, ".subsection %d\n", subsection_index);
1545 const char *get_label (const char *s)
1547 #ifdef TARGET_ASM_APPLE
1548 if (s [0] == '.' && s [1] == 'L')
1549 /* apple uses "L" instead of ".L" to mark temporary labels */
1556 asm_writer_emit_symbol_type (MonoImageWriter *acfg, const char *name, gboolean func)
1565 asm_writer_emit_unset_mode (acfg);
1566 #if defined(TARGET_ASM_APPLE)
1568 #elif defined(TARGET_ARM)
1569 fprintf (acfg->fp, "\t.type %s,#%s\n", name, stype);
1571 fprintf (acfg->fp, "\t.type %s,@%s\n", name, stype);
1576 asm_writer_emit_global (MonoImageWriter *acfg, const char *name, gboolean func)
1578 asm_writer_emit_unset_mode (acfg);
1579 #if ((defined(__ppc__) || defined(TARGET_X86)) && defined(TARGET_ASM_APPLE)) || (defined(HOST_WIN32) && !defined(MONO_CROSS_COMPILE))
1580 // mach-o always uses a '_' prefix.
1581 fprintf (acfg->fp, "\t.globl _%s\n", name);
1583 fprintf (acfg->fp, "\t.globl %s\n", name);
1586 asm_writer_emit_symbol_type (acfg, name, func);
1590 asm_writer_emit_local_symbol (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean func)
1592 asm_writer_emit_unset_mode (acfg);
1594 #ifndef TARGET_ASM_APPLE
1595 fprintf (acfg->fp, "\t.local %s\n", name);
1598 asm_writer_emit_symbol_type (acfg, name, func);
1602 asm_writer_emit_symbol_size (MonoImageWriter *acfg, const char *name, const char *end_label)
1604 asm_writer_emit_unset_mode (acfg);
1606 #ifndef TARGET_ASM_APPLE
1607 fprintf (acfg->fp, "\t.size %s,%s-%s\n", name, end_label, name);
1612 asm_writer_emit_label (MonoImageWriter *acfg, const char *name)
1614 asm_writer_emit_unset_mode (acfg);
1615 #if (defined(TARGET_X86) && defined(TARGET_ASM_APPLE))
1616 name = get_label(name);
1617 fprintf (acfg->fp, "%s:\n", name);
1619 fprintf (acfg->fp, "_%s:\n", name);
1621 #elif (defined(HOST_WIN32) && (defined(TARGET_X86) || defined(TARGET_AMD64))) || (defined(TARGET_X86) && defined(TARGET_ASM_APPLE))
1622 fprintf (acfg->fp, "_%s:\n", name);
1623 #if defined(HOST_WIN32)
1624 /* Emit a normal label too */
1625 fprintf (acfg->fp, "%s:\n", name);
1628 fprintf (acfg->fp, "%s:\n", get_label (name));
1634 asm_writer_emit_string (MonoImageWriter *acfg, const char *value)
1636 asm_writer_emit_unset_mode (acfg);
1637 fprintf (acfg->fp, "\t%s \"%s\"\n", AS_STRING_DIRECTIVE, value);
1641 asm_writer_emit_line (MonoImageWriter *acfg)
1643 asm_writer_emit_unset_mode (acfg);
1644 fprintf (acfg->fp, "\n");
1648 asm_writer_emit_alignment (MonoImageWriter *acfg, int size)
1650 asm_writer_emit_unset_mode (acfg);
1651 #if defined(TARGET_ARM)
1652 fprintf (acfg->fp, "\t.align %d\n", ilog2 (size));
1653 #elif defined(__ppc__) && defined(TARGET_ASM_APPLE)
1654 // the mach-o assembler specifies alignments as powers of 2.
1655 fprintf (acfg->fp, "\t.align %d\t; ilog2\n", ilog2(size));
1656 #elif defined(TARGET_ASM_GAS)
1657 fprintf (acfg->fp, "\t.balign %d\n", size);
1658 #elif defined(TARGET_ASM_APPLE)
1659 fprintf (acfg->fp, "\t.align %d\n", ilog2 (size));
1661 fprintf (acfg->fp, "\t.align %d\n", size);
1665 #ifdef __native_client_codegen__
1667 asm_writer_emit_nacl_call_alignment (MonoImageWriter *acfg) {
1668 int padding = kNaClAlignment - kNaClLengthOfCallImm;
1669 guint8 padc = '\x90';
1671 fprintf (acfg->fp, "\n\t.align %d", kNaClAlignment);
1672 while (padding > 0) {
1673 fprintf (acfg->fp, "\n\t.byte %d", padc);
1677 #endif /* __native_client_codegen__ */
1680 asm_writer_emit_pointer_unaligned (MonoImageWriter *acfg, const char *target)
1682 asm_writer_emit_unset_mode (acfg);
1683 fprintf (acfg->fp, "\t%s %s\n", AS_POINTER_DIRECTIVE, target ? target : "0");
1687 asm_writer_emit_pointer (MonoImageWriter *acfg, const char *target)
1689 asm_writer_emit_unset_mode (acfg);
1690 asm_writer_emit_alignment (acfg, sizeof (gpointer));
1691 asm_writer_emit_pointer_unaligned (acfg, target);
1694 static char *byte_to_str;
1697 asm_writer_emit_bytes (MonoImageWriter *acfg, const guint8* buf, int size)
1700 if (acfg->mode != EMIT_BYTE) {
1701 acfg->mode = EMIT_BYTE;
1702 acfg->col_count = 0;
1705 if (byte_to_str == NULL) {
1706 byte_to_str = g_new0 (char, 256 * 8);
1707 for (i = 0; i < 256; ++i) {
1708 sprintf (byte_to_str + (i * 8), ",%d", i);
1712 for (i = 0; i < size; ++i, ++acfg->col_count) {
1713 if ((acfg->col_count % 32) == 0)
1714 fprintf (acfg->fp, "\n\t.byte %d", buf [i]);
1716 fputs (byte_to_str + (buf [i] * 8), acfg->fp);
1721 asm_writer_emit_int16 (MonoImageWriter *acfg, int value)
1723 if (acfg->mode != EMIT_WORD) {
1724 acfg->mode = EMIT_WORD;
1725 acfg->col_count = 0;
1727 if ((acfg->col_count++ % 8) == 0)
1728 fprintf (acfg->fp, "\n\t%s ", AS_INT16_DIRECTIVE);
1730 fprintf (acfg->fp, ", ");
1731 fprintf (acfg->fp, "%d", value);
1735 asm_writer_emit_int32 (MonoImageWriter *acfg, int value)
1737 if (acfg->mode != EMIT_LONG) {
1738 acfg->mode = EMIT_LONG;
1739 acfg->col_count = 0;
1741 if ((acfg->col_count++ % 8) == 0)
1742 fprintf (acfg->fp, "\n\t%s ", AS_INT32_DIRECTIVE);
1744 fprintf (acfg->fp, ",");
1745 fprintf (acfg->fp, "%d", value);
1749 asm_writer_emit_symbol_diff (MonoImageWriter *acfg, const char *end, const char* start, int offset)
1751 #ifdef TARGET_ASM_APPLE
1755 if (acfg->mode != EMIT_LONG) {
1756 acfg->mode = EMIT_LONG;
1757 acfg->col_count = 0;
1760 // FIXME: This doesn't seem to work on the iphone
1762 //#ifdef TARGET_ASM_APPLE
1763 /* The apple assembler needs a separate symbol to be able to handle complex expressions */
1764 sprintf (symbol, "LTMP_SYM%d", acfg->label_gen);
1765 start = get_label (start);
1766 end = get_label (end);
1769 fprintf (acfg->fp, "\n%s=%s - %s + %d", symbol, end, start, offset);
1770 else if (offset < 0)
1771 fprintf (acfg->fp, "\n%s=%s - %s %d", symbol, end, start, offset);
1773 fprintf (acfg->fp, "\n%s=%s - %s", symbol, end, start);
1775 fprintf (acfg->fp, "\n\t%s ", AS_INT32_DIRECTIVE);
1776 fprintf (acfg->fp, "%s", symbol);
1778 start = get_label (start);
1779 end = get_label (end);
1780 if ((acfg->col_count++ % 8) == 0)
1781 fprintf (acfg->fp, "\n\t%s ", AS_INT32_DIRECTIVE);
1783 fprintf (acfg->fp, ",");
1785 fprintf (acfg->fp, "%s - %s + %d", end, start, offset);
1786 else if (offset < 0)
1787 fprintf (acfg->fp, "%s - %s %d", end, start, offset);
1789 fprintf (acfg->fp, "%s - %s", end, start);
1794 asm_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
1796 asm_writer_emit_unset_mode (acfg);
1797 fprintf (acfg->fp, "\t%s %d\n", AS_SKIP_DIRECTIVE, num);
1800 /* EMIT FUNCTIONS */
1803 img_writer_emit_start (MonoImageWriter *acfg)
1805 #ifdef USE_BIN_WRITER
1806 if (acfg->use_bin_writer)
1807 bin_writer_emit_start (acfg);
1809 asm_writer_emit_start (acfg);
1811 asm_writer_emit_start (acfg);
1816 img_writer_emit_section_change (MonoImageWriter *acfg, const char *section_name, int subsection_index)
1818 #ifdef USE_BIN_WRITER
1819 if (acfg->use_bin_writer)
1820 bin_writer_emit_section_change (acfg, section_name, subsection_index);
1822 asm_writer_emit_section_change (acfg, section_name, subsection_index);
1824 asm_writer_emit_section_change (acfg, section_name, subsection_index);
1827 acfg->current_section = section_name;
1828 acfg->current_subsection = subsection_index;
1832 img_writer_emit_push_section (MonoImageWriter *acfg, const char *section_name, int subsection)
1834 g_assert (acfg->stack_pos < 16 - 1);
1835 acfg->section_stack [acfg->stack_pos] = acfg->current_section;
1836 acfg->subsection_stack [acfg->stack_pos] = acfg->current_subsection;
1839 img_writer_emit_section_change (acfg, section_name, subsection);
1843 img_writer_emit_pop_section (MonoImageWriter *acfg)
1845 g_assert (acfg->stack_pos > 0);
1847 img_writer_emit_section_change (acfg, acfg->section_stack [acfg->stack_pos], acfg->subsection_stack [acfg->stack_pos]);
1851 img_writer_set_section_addr (MonoImageWriter *acfg, guint64 addr)
1853 #ifdef USE_BIN_WRITER
1854 if (!acfg->use_bin_writer)
1857 bin_writer_set_section_addr (acfg, addr);
1864 img_writer_emit_global (MonoImageWriter *acfg, const char *name, gboolean func)
1866 #ifdef USE_BIN_WRITER
1867 if (acfg->use_bin_writer)
1868 bin_writer_emit_global (acfg, name, func);
1870 asm_writer_emit_global (acfg, name, func);
1872 asm_writer_emit_global (acfg, name, func);
1877 img_writer_emit_local_symbol (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean func)
1879 #ifdef USE_BIN_WRITER
1880 if (acfg->use_bin_writer)
1881 bin_writer_emit_local_symbol (acfg, name, end_label, func);
1883 asm_writer_emit_local_symbol (acfg, name, end_label, func);
1885 asm_writer_emit_local_symbol (acfg, name, end_label, func);
1890 img_writer_emit_symbol_size (MonoImageWriter *acfg, const char *name, const char *end_label)
1892 if (!acfg->use_bin_writer)
1893 asm_writer_emit_symbol_size (acfg, name, end_label);
1897 img_writer_emit_label (MonoImageWriter *acfg, const char *name)
1899 #ifdef USE_BIN_WRITER
1900 if (acfg->use_bin_writer)
1901 bin_writer_emit_label (acfg, name);
1903 asm_writer_emit_label (acfg, name);
1905 asm_writer_emit_label (acfg, name);
1910 img_writer_emit_bytes (MonoImageWriter *acfg, const guint8* buf, int size)
1912 #ifdef USE_BIN_WRITER
1913 if (acfg->use_bin_writer)
1914 bin_writer_emit_bytes (acfg, buf, size);
1916 asm_writer_emit_bytes (acfg, buf, size);
1918 asm_writer_emit_bytes (acfg, buf, size);
1923 img_writer_emit_string (MonoImageWriter *acfg, const char *value)
1925 #ifdef USE_BIN_WRITER
1926 if (acfg->use_bin_writer)
1927 bin_writer_emit_string (acfg, value);
1929 asm_writer_emit_string (acfg, value);
1931 asm_writer_emit_string (acfg, value);
1936 img_writer_emit_line (MonoImageWriter *acfg)
1938 #ifdef USE_BIN_WRITER
1939 if (acfg->use_bin_writer)
1940 bin_writer_emit_line (acfg);
1942 asm_writer_emit_line (acfg);
1944 asm_writer_emit_line (acfg);
1949 img_writer_emit_alignment (MonoImageWriter *acfg, int size)
1951 #ifdef USE_BIN_WRITER
1952 if (acfg->use_bin_writer)
1953 bin_writer_emit_alignment (acfg, size);
1955 asm_writer_emit_alignment (acfg, size);
1957 asm_writer_emit_alignment (acfg, size);
1961 #ifdef __native_client_codegen__
1963 img_writer_emit_nacl_call_alignment (MonoImageWriter *acfg) {
1964 #ifdef USE_BIN_WRITER
1965 if (acfg->use_bin_writer)
1966 bin_writer_emit_nacl_call_alignment (acfg);
1968 asm_writer_emit_nacl_call_alignment (acfg);
1970 g_assert_not_reached();
1973 #endif /* __native_client_codegen__ */
1976 img_writer_emit_pointer_unaligned (MonoImageWriter *acfg, const char *target)
1978 #ifdef USE_BIN_WRITER
1979 if (acfg->use_bin_writer)
1980 bin_writer_emit_pointer_unaligned (acfg, target);
1982 asm_writer_emit_pointer_unaligned (acfg, target);
1984 asm_writer_emit_pointer_unaligned (acfg, target);
1989 img_writer_emit_pointer (MonoImageWriter *acfg, const char *target)
1991 #ifdef USE_BIN_WRITER
1992 if (acfg->use_bin_writer)
1993 bin_writer_emit_pointer (acfg, target);
1995 asm_writer_emit_pointer (acfg, target);
1997 asm_writer_emit_pointer (acfg, target);
2002 img_writer_emit_int16 (MonoImageWriter *acfg, int value)
2004 #ifdef USE_BIN_WRITER
2005 if (acfg->use_bin_writer)
2006 bin_writer_emit_int16 (acfg, value);
2008 asm_writer_emit_int16 (acfg, value);
2010 asm_writer_emit_int16 (acfg, value);
2015 img_writer_emit_int32 (MonoImageWriter *acfg, int value)
2017 #ifdef USE_BIN_WRITER
2018 if (acfg->use_bin_writer)
2019 bin_writer_emit_int32 (acfg, value);
2021 asm_writer_emit_int32 (acfg, value);
2023 asm_writer_emit_int32 (acfg, value);
2028 img_writer_emit_symbol_diff (MonoImageWriter *acfg, const char *end, const char* start, int offset)
2030 #ifdef USE_BIN_WRITER
2031 if (acfg->use_bin_writer)
2032 bin_writer_emit_symbol_diff (acfg, end, start, offset);
2034 asm_writer_emit_symbol_diff (acfg, end, start, offset);
2036 asm_writer_emit_symbol_diff (acfg, end, start, offset);
2041 img_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
2043 #ifdef USE_BIN_WRITER
2044 if (acfg->use_bin_writer)
2045 bin_writer_emit_zero_bytes (acfg, num);
2047 asm_writer_emit_zero_bytes (acfg, num);
2049 asm_writer_emit_zero_bytes (acfg, num);
2054 img_writer_emit_writeout (MonoImageWriter *acfg)
2056 #ifdef USE_BIN_WRITER
2057 if (acfg->use_bin_writer)
2058 return bin_writer_emit_writeout (acfg);
2060 return asm_writer_emit_writeout (acfg);
2062 return asm_writer_emit_writeout (acfg);
2067 img_writer_emit_byte (MonoImageWriter *acfg, guint8 val)
2069 img_writer_emit_bytes (acfg, &val, 1);
2073 * Emit a relocation entry of type RELOC_TYPE against symbol SYMBOL at the current PC.
2074 * Do not advance PC.
2077 img_writer_emit_reloc (MonoImageWriter *acfg, int reloc_type, const char *symbol, int addend)
2079 /* This is only supported by the bin writer */
2080 #ifdef USE_BIN_WRITER
2081 if (acfg->use_bin_writer)
2082 bin_writer_emit_reloc (acfg, reloc_type, symbol, addend);
2084 g_assert_not_reached ();
2086 g_assert_not_reached ();
2091 * img_writer_emit_unset_mode:
2093 * Flush buffered data so it is safe to write to the output file from outside this
2094 * module. This is a nop for the binary writer.
2097 img_writer_emit_unset_mode (MonoImageWriter *acfg)
2099 if (!acfg->use_bin_writer)
2100 asm_writer_emit_unset_mode (acfg);
2104 * img_writer_get_output:
2106 * Return the output buffer of a binary writer emitting to memory. The returned memory
2107 * is from malloc, and it is owned by the caller.
2110 img_writer_get_output (MonoImageWriter *acfg, guint32 *size)
2112 #ifdef USE_BIN_WRITER
2115 g_assert (acfg->use_bin_writer);
2117 buf = acfg->out_buf;
2118 *size = acfg->out_buf_size;
2119 acfg->out_buf = NULL;
2122 g_assert_not_reached ();
2128 * Return whenever the binary writer is supported on this platform.
2131 bin_writer_supported (void)
2133 #ifdef USE_BIN_WRITER
2141 * img_writer_create:
2143 * Create an image writer writing to FP. If USE_BIN_WRITER is TRUE, FP can be NULL,
2144 * in this case the image writer will write to a memory buffer obtainable by calling
2145 * img_writer_get_output ().
2148 img_writer_create (FILE *fp, gboolean use_bin_writer)
2150 MonoImageWriter *w = g_new0 (MonoImageWriter, 1);
2152 #ifndef USE_BIN_WRITER
2153 g_assert (!use_bin_writer);
2156 if (!use_bin_writer)
2160 w->use_bin_writer = use_bin_writer;
2161 w->mempool = mono_mempool_new ();
2167 img_writer_destroy (MonoImageWriter *w)
2169 // FIXME: Free all the stuff
2170 mono_mempool_destroy (w->mempool);
2175 img_writer_subsections_supported (MonoImageWriter *acfg)
2177 #ifdef TARGET_ASM_APPLE
2178 return acfg->use_bin_writer;
2185 img_writer_get_fp (MonoImageWriter *acfg)
2191 img_writer_get_temp_label_prefix (MonoImageWriter *acfg)
2193 return AS_TEMP_LABEL_PREFIX;