2 * image-writer.c: Creation of object files or assembly files using the same interface.
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Zoltan Varga (vargaz@gmail.com)
7 * Paolo Molaro (lupus@ximian.com)
9 * (C) 2002 Ximian, Inc.
13 #include <sys/types.h>
32 #include <limits.h> /* for PAGESIZE */
37 #include "image-writer.h"
40 #include <mono/utils/freebsd-elf32.h>
41 #include <mono/utils/freebsd-elf64.h>
46 #define TV_DECLARE(name) gint64 name
47 #define TV_GETTIME(tv) tv = mono_100ns_ticks ()
48 #define TV_ELAPSED(start,end) (((end) - (start)) / 10)
51 * The used assembler dialect
52 * TARGET_ASM_APPLE == apple assembler on OSX
53 * TARGET_ASM_GAS == GNU assembler
55 #if !defined(TARGET_ASM_APPLE) && !defined(TARGET_ASM_GAS)
56 #if defined(TARGET_MACH) && !defined(__native_client_codegen__)
57 #define TARGET_ASM_APPLE
59 #define TARGET_ASM_GAS
64 * Defines for the directives used by different assemblers
66 #if defined(TARGET_POWERPC) || defined(TARGET_MACH)
67 #define AS_STRING_DIRECTIVE ".asciz"
69 #define AS_STRING_DIRECTIVE ".string"
72 #define AS_INT32_DIRECTIVE ".long"
73 #define AS_INT64_DIRECTIVE ".quad"
75 #if (defined(TARGET_AMD64) || defined(TARGET_POWERPC64)) && !defined(__mono_ilp32__)
76 #define AS_POINTER_DIRECTIVE ".quad"
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(TARGET_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)
385 acfg->cur_section->cur_offset += sizeof (gpointer);
389 reloc = g_new0 (BinReloc, 1);
390 reloc->val1 = g_strdup (target);
391 reloc->section = acfg->cur_section;
392 reloc->section_offset = acfg->cur_section->cur_offset;
393 reloc->next = acfg->relocations;
394 acfg->relocations = reloc;
395 if (strcmp (reloc->section->name, ".data") == 0) {
397 //g_print ("reloc: %s at %d\n", target, acfg->cur_section->cur_offset);
399 acfg->cur_section->cur_offset += sizeof (gpointer);
403 bin_writer_emit_pointer (MonoImageWriter *acfg, const char *target)
405 bin_writer_emit_alignment (acfg, sizeof (gpointer));
406 bin_writer_emit_pointer_unaligned (acfg, target);
410 bin_writer_emit_int16 (MonoImageWriter *acfg, int value)
413 bin_writer_emit_ensure_buffer (acfg->cur_section, 2);
414 data = acfg->cur_section->data + acfg->cur_section->cur_offset;
415 acfg->cur_section->cur_offset += 2;
416 /* FIXME: little endian */
418 data [1] = value >> 8;
422 bin_writer_emit_int32 (MonoImageWriter *acfg, int value)
425 bin_writer_emit_ensure_buffer (acfg->cur_section, 4);
426 data = acfg->cur_section->data + acfg->cur_section->cur_offset;
427 acfg->cur_section->cur_offset += 4;
428 /* FIXME: little endian */
430 data [1] = value >> 8;
431 data [2] = value >> 16;
432 data [3] = value >> 24;
436 create_reloc (MonoImageWriter *acfg, const char *end, const char* start, int offset)
439 reloc = mono_mempool_alloc0 (acfg->mempool, sizeof (BinReloc));
440 reloc->val1 = mono_mempool_strdup (acfg->mempool, end);
441 if (strcmp (start, ".") == 0) {
442 reloc->val2_section = acfg->cur_section;
443 reloc->val2_offset = acfg->cur_section->cur_offset;
445 reloc->val2 = mono_mempool_strdup (acfg->mempool, start);
447 reloc->offset = offset;
448 reloc->section = acfg->cur_section;
449 reloc->section_offset = acfg->cur_section->cur_offset;
450 reloc->next = acfg->relocations;
451 acfg->relocations = reloc;
456 bin_writer_emit_symbol_diff (MonoImageWriter *acfg, const char *end, const char* start, int offset)
458 create_reloc (acfg, end, start, offset);
459 acfg->cur_section->cur_offset += 4;
460 /*if (strcmp (reloc->section->name, ".data") == 0) {
462 g_print ("reloc: %s - %s + %d at %d\n", end, start, offset, acfg->cur_section->cur_offset - 4);
467 * Emit a relocation entry of type RELOC_TYPE against symbol SYMBOL at the current PC.
470 static G_GNUC_UNUSED void
471 bin_writer_emit_reloc (MonoImageWriter *acfg, int reloc_type, const char *symbol, int addend)
473 BinReloc *reloc = create_reloc (acfg, symbol, ".", addend);
474 reloc->reloc_type = reloc_type;
478 bin_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
480 bin_writer_emit_ensure_buffer (acfg->cur_section, num);
481 acfg->cur_section->cur_offset += num;
484 #ifdef USE_ELF_WRITER
510 #if SIZEOF_VOID_P == 4
512 typedef Elf32_Ehdr ElfHeader;
513 typedef Elf32_Shdr ElfSectHeader;
514 typedef Elf32_Phdr ElfProgHeader;
515 typedef Elf32_Sym ElfSymbol;
516 typedef Elf32_Rel ElfReloc;
517 typedef Elf32_Rela ElfRelocA;
518 typedef Elf32_Dyn ElfDynamic;
522 typedef Elf64_Ehdr ElfHeader;
523 typedef Elf64_Shdr ElfSectHeader;
524 typedef Elf64_Phdr ElfProgHeader;
525 typedef Elf64_Sym ElfSymbol;
526 typedef Elf64_Rel ElfReloc;
527 typedef Elf64_Rela ElfRelocA;
528 typedef Elf64_Dyn ElfDynamic;
540 static SectInfo section_info [] = {
542 {".hash", SHT_HASH, 4, 2, SIZEOF_VOID_P},
543 {".dynsym", SHT_DYNSYM, sizeof (ElfSymbol), 2, SIZEOF_VOID_P},
544 {".dynstr", SHT_STRTAB, 0, 2, 1},
545 {".rel.dyn", SHT_REL, sizeof (ElfReloc), 2, SIZEOF_VOID_P},
546 {".rela.dyn", SHT_RELA, sizeof (ElfRelocA), 2, SIZEOF_VOID_P},
547 {".text", SHT_PROGBITS, 0, 6, 4096},
548 {".rodata", SHT_PROGBITS, 0, SHF_ALLOC, 4096},
549 {".dynamic", SHT_DYNAMIC, sizeof (ElfDynamic), 3, SIZEOF_VOID_P},
550 {".got.plt", SHT_PROGBITS, SIZEOF_VOID_P, 3, SIZEOF_VOID_P},
551 {".data", SHT_PROGBITS, 0, 3, 8},
552 {".bss", SHT_NOBITS, 0, 3, 8},
553 {".debug_frame", SHT_PROGBITS, 0, 0, 8},
554 {".debug_info", SHT_PROGBITS, 0, 0, 1},
555 {".debug_abbrev", SHT_PROGBITS, 0, 0, 1},
556 {".debug_line", SHT_PROGBITS, 0, 0, 1},
557 {".debug_loc", SHT_PROGBITS, 0, 0, 1},
558 {".shstrtab", SHT_STRTAB, 0, 0, 1},
559 {".symtab", SHT_SYMTAB, sizeof (ElfSymbol), 0, SIZEOF_VOID_P},
560 {".strtab", SHT_STRTAB, 0, 0, 1}
569 str_table_add (ElfStrTable *table, const char* value)
573 table->data = g_string_new_len ("", 1);
574 table->hash = g_hash_table_new (g_str_hash, g_str_equal);
576 idx = GPOINTER_TO_UINT (g_hash_table_lookup (table->hash, value));
579 idx = table->data->len;
580 g_string_append (table->data, value);
581 g_string_append_c (table->data, 0);
582 g_hash_table_insert (table->hash, (void*)value, GUINT_TO_POINTER (idx));
587 append_subsection (MonoImageWriter *acfg, ElfSectHeader *sheaders, BinSection *sect, BinSection *add)
589 int offset = sect->cur_offset;
590 /*offset += (sheaders [sect->shidx].sh_addralign - 1);
591 offset &= ~(sheaders [sect->shidx].sh_addralign - 1);*/
593 * FIXME: we shouldn't align subsections at all, but if we don't then the
594 * stuff inside the subsections which is aligned won't get aligned.
596 if (strcmp (sect->name, ".debug_line") != 0) {
600 bin_writer_emit_ensure_buffer (sect, offset);
601 //g_print ("section %s aligned to %d from %d\n", sect->name, offset, sect->cur_offset);
602 sect->cur_offset = offset;
604 bin_writer_emit_ensure_buffer (sect, add->cur_offset);
605 memcpy (sect->data + sect->cur_offset, add->data, add->cur_offset);
607 sect->cur_offset += add->cur_offset;
608 add->cur_offset = offset; /* it becomes the offset in the parent section */
609 //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);
614 /* merge the subsections */
616 collect_sections (MonoImageWriter *acfg, ElfSectHeader *sheaders, BinSection **out, int num)
618 int i, j, maxs, num_sections;
623 for (sect = acfg->sections; sect; sect = sect->next) {
624 if (sect->subsection == 0) {
625 out [num_sections++] = sect;
626 g_assert (num_sections < num);
628 maxs = MAX (maxs, sect->subsection);
630 for (i = 0; i < num_sections; i++) {
631 for (j = 1; j <= maxs; ++j) {
632 for (sect = acfg->sections; sect; sect = sect->next) {
633 if (sect->subsection == j && strcmp (out [i]->name, sect->name) == 0) {
634 append_subsection (acfg, sheaders, out [i], sect);
643 elf_hash (const unsigned char *name)
645 unsigned long h = 0, g;
647 h = (h << 4) + *name++;
648 if ((g = h & 0xf0000000))
655 #define NUM_BUCKETS 17
658 build_hash (MonoImageWriter *acfg, int num_sections, ElfStrTable *dynstr)
661 int num_symbols = 1 + num_sections + 3;
664 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
665 if (!symbol->is_global)
668 str_table_add (dynstr, symbol->name);
669 /*g_print ("adding sym: %s\n", symbol->name);*/
671 str_table_add (dynstr, "__bss_start");
672 str_table_add (dynstr, "_edata");
673 str_table_add (dynstr, "_end");
675 data = g_new0 (int, num_symbols + 2 + NUM_BUCKETS);
676 data [0] = NUM_BUCKETS;
677 data [1] = num_symbols;
683 get_label_addr (MonoImageWriter *acfg, const char *name)
690 lab = g_hash_table_lookup (acfg->labels, name);
692 g_error ("Undefined label: '%s'.\n", name);
693 section = lab->section;
694 offset = lab->offset;
695 if (section->parent) {
696 value = section->parent->virt_offset + section->cur_offset + offset;
698 value = section->virt_offset + offset;
704 collect_syms (MonoImageWriter *acfg, int *hash, ElfStrTable *strtab, ElfSectHeader *sheaders, int *num_syms)
715 symbols = g_new0 (ElfSymbol, hash [1]);
718 for (symbol = acfg->symbols; symbol; symbol = symbol->next)
721 symbols = g_new0 (ElfSymbol, i + SECT_NUM + 10); /* FIXME */
724 /* the first symbol is undef, all zeroes */
728 for (j = 1; j < SECT_NUM; ++j) {
729 symbols [i].st_info = ELF32_ST_INFO (STB_LOCAL, STT_SECTION);
730 symbols [i].st_shndx = j;
731 symbols [i].st_value = sheaders [j].sh_addr;
735 for (section = acfg->sections; section; section = section->next) {
738 symbols [i].st_info = ELF32_ST_INFO (STB_LOCAL, STT_SECTION);
739 if (strcmp (section->name, ".text") == 0) {
740 symbols [i].st_shndx = SECT_TEXT;
741 section->shidx = SECT_TEXT;
742 section->file_offset = 4096;
743 symbols [i].st_value = section->virt_offset;
744 } else if (strcmp (section->name, ".rodata") == 0) {
745 symbols [i].st_shndx = SECT_RODATA;
746 section->shidx = SECT_RODATA;
747 section->file_offset = 4096;
748 symbols [i].st_value = section->virt_offset;
749 } else if (strcmp (section->name, ".data") == 0) {
750 symbols [i].st_shndx = SECT_DATA;
751 section->shidx = SECT_DATA;
752 section->file_offset = 4096 + 28; /* FIXME */
753 symbols [i].st_value = section->virt_offset;
754 } else if (strcmp (section->name, ".bss") == 0) {
755 symbols [i].st_shndx = SECT_BSS;
756 section->shidx = SECT_BSS;
757 section->file_offset = 4096 + 28 + 8; /* FIXME */
758 symbols [i].st_value = section->virt_offset;
763 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
766 if (!symbol->is_global && hash)
768 symbols [i].st_info = ELF32_ST_INFO (symbol->is_global ? STB_GLOBAL : STB_LOCAL, symbol->is_function? STT_FUNC : STT_OBJECT);
769 symbols [i].st_name = str_table_add (strtab, symbol->name);
770 /*g_print ("sym name %s tabled to %d\n", symbol->name, symbols [i].st_name);*/
771 section = symbol->section;
772 symbols [i].st_shndx = section->parent? section->parent->shidx: section->shidx;
773 lab = g_hash_table_lookup (acfg->labels, symbol->name);
774 offset = lab->offset;
775 if (section->parent) {
776 symbols [i].st_value = section->parent->virt_offset + section->cur_offset + offset;
778 symbols [i].st_value = section->virt_offset + offset;
781 if (symbol->end_label) {
782 BinLabel *elab = g_hash_table_lookup (acfg->labels, symbol->end_label);
784 symbols [i].st_size = elab->offset - lab->offset;
788 /* add special symbols */
789 symbols [i].st_name = str_table_add (strtab, "__bss_start");
790 symbols [i].st_shndx = 0xfff1;
791 symbols [i].st_info = ELF32_ST_INFO (STB_GLOBAL, 0);
793 symbols [i].st_name = str_table_add (strtab, "_edata");
794 symbols [i].st_shndx = 0xfff1;
795 symbols [i].st_info = ELF32_ST_INFO (STB_GLOBAL, 0);
797 symbols [i].st_name = str_table_add (strtab, "_end");
798 symbols [i].st_shndx = 0xfff1;
799 symbols [i].st_info = ELF32_ST_INFO (STB_GLOBAL, 0);
805 /* add to hash table */
808 chain = hash + 2 + hash [0];
809 for (i = 0; i < hash [1]; ++i) {
811 /*g_print ("checking %d '%s' (sym %d)\n", symbols [i].st_name, strtab->data->str + symbols [i].st_name, i);*/
812 if (!symbols [i].st_name)
814 hashc = elf_hash ((guint8*)strtab->data->str + symbols [i].st_name);
815 slot = hashc % hash [0];
816 /*g_print ("hashing '%s' at slot %d (sym %d)\n", strtab->data->str + symbols [i].st_name, slot, i);*/
818 chain [i] = bucket [slot];
829 reloc_symbols (MonoImageWriter *acfg, ElfSymbol *symbols, ElfSectHeader *sheaders, ElfStrTable *strtab, gboolean dynamic)
837 for (section = acfg->sections; section; section = section->next) {
840 symbols [i].st_value = sheaders [section->shidx].sh_addr;
844 for (i = 1; i < SECT_NUM; ++i) {
845 symbols [i].st_value = sheaders [i].sh_addr;
848 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
851 if (dynamic && !symbol->is_global)
853 section = symbol->section;
854 lab = g_hash_table_lookup (acfg->labels, symbol->name);
855 offset = lab->offset;
856 if (section->parent) {
857 symbols [i].st_value = sheaders [section->parent->shidx].sh_addr + section->cur_offset + offset;
859 symbols [i].st_value = sheaders [section->shidx].sh_addr + offset;
864 symbols [i].st_value = sheaders [SECT_BSS].sh_addr;
867 symbols [i].st_value = sheaders [SECT_DATA].sh_addr + sheaders [SECT_DATA].sh_size;
870 symbols [i].st_value = sheaders [SECT_BSS].sh_addr + sheaders [SECT_BSS].sh_size;
875 resolve_reloc (MonoImageWriter *acfg, BinReloc *reloc, guint8 **out_data, gsize *out_vaddr, gsize *out_start_val, gsize *out_end_val)
878 gssize end_val, start_val;
881 end_val = get_label_addr (acfg, reloc->val1);
883 start_val = get_label_addr (acfg, reloc->val2);
884 } else if (reloc->val2_section) {
885 start_val = reloc->val2_offset;
886 if (reloc->val2_section->parent)
887 start_val += reloc->val2_section->parent->virt_offset + reloc->val2_section->cur_offset;
889 start_val += reloc->val2_section->virt_offset;
893 end_val = end_val - start_val + reloc->offset;
894 if (reloc->section->parent) {
895 data = reloc->section->parent->data;
896 data += reloc->section->cur_offset;
897 data += reloc->section_offset;
898 vaddr = reloc->section->parent->virt_offset;
899 vaddr += reloc->section->cur_offset;
900 vaddr += reloc->section_offset;
902 data = reloc->section->data;
903 data += reloc->section_offset;
904 vaddr = reloc->section->virt_offset;
905 vaddr += reloc->section_offset;
908 *out_start_val = start_val;
909 *out_end_val = end_val;
917 resolve_relocations (MonoImageWriter *acfg)
921 gsize end_val, start_val;
926 rr = g_new0 (ElfRelocA, acfg->num_relocs);
929 for (reloc = acfg->relocations; reloc; reloc = reloc->next) {
930 resolve_reloc (acfg, reloc, &data, &vaddr, &start_val, &end_val);
931 /* FIXME: little endian */
933 data [1] = end_val >> 8;
934 data [2] = end_val >> 16;
935 data [3] = end_val >> 24;
937 if (start_val == 0 && reloc->val1 [0] != '.') {
938 rr [i].r_offset = vaddr;
939 rr [i].r_info = R_X86_64_RELATIVE;
940 rr [i].r_addend = end_val;
942 g_assert (i <= acfg->num_relocs);
948 #else /* USE_ELF_RELA */
951 do_reloc (MonoImageWriter *acfg, BinReloc *reloc, guint8 *data, gssize addr)
955 * We use the official ARM relocation types, but implement only the stuff actually
956 * needed by the code we generate.
958 switch (reloc->reloc_type) {
961 guint32 *code = (guint32*)(gpointer)data;
965 if (reloc->reloc_type == R_ARM_CALL)
967 g_assert (data [3] == 0xeb);
970 g_assert (data [3] == 0xea);
971 if (diff >= 0 && diff <= 33554431) {
973 ins = (ins & 0xff000000) | diff;
975 } else if (diff <= 0 && diff >= -33554432) {
977 ins = (ins & 0xff000000) | (diff & ~0xff000000);
980 g_assert_not_reached ();
984 case R_ARM_ALU_PC_G0_NC: {
985 /* Generated by emit_plt () */
989 g_assert (val <= 0xffffff);
991 ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_PC, (val & 0xFF0000) >> 16, 16);
993 ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_PC, 0, 0);
994 ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_IP, (val & 0xFF00) >> 8, 24);
995 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, val & 0xFF);
999 g_assert_not_reached ();
1002 g_assert_not_reached ();
1007 resolve_relocations (MonoImageWriter *acfg)
1011 gsize end_val, start_val;
1016 rr = g_new0 (ElfReloc, acfg->num_relocs);
1019 for (reloc = acfg->relocations; reloc; reloc = reloc->next) {
1020 resolve_reloc (acfg, reloc, &data, &vaddr, &start_val, &end_val);
1021 /* FIXME: little endian */
1022 if (reloc->reloc_type) {
1023 /* Must be static */
1024 g_assert (start_val > 0);
1025 do_reloc (acfg, reloc, data, end_val);
1028 data [1] = end_val >> 8;
1029 data [2] = end_val >> 16;
1030 data [3] = end_val >> 24;
1033 if (start_val == 0 && reloc->val1 [0] != '.') {
1034 rr [i].r_offset = vaddr;
1035 rr [i].r_info = R_386_RELATIVE;
1037 g_assert (i <= acfg->num_relocs);
1043 #endif /* USE_ELF_RELA */
1046 bin_writer_fwrite (MonoImageWriter *acfg, void *val, size_t size, size_t nmemb)
1049 fwrite (val, size, nmemb, acfg->fp);
1051 g_assert (acfg->out_buf_pos + (size * nmemb) <= acfg->out_buf_size);
1052 memcpy (acfg->out_buf + acfg->out_buf_pos, val, size * nmemb);
1053 acfg->out_buf_pos += (size * nmemb);
1058 bin_writer_fseek (MonoImageWriter *acfg, int offset)
1061 fseek (acfg->fp, offset, SEEK_SET);
1063 acfg->out_buf_pos = offset;
1066 static int normal_sections [] = { SECT_DATA, SECT_DEBUG_FRAME, SECT_DEBUG_INFO, SECT_DEBUG_ABBREV, SECT_DEBUG_LINE, SECT_DEBUG_LOC };
1069 bin_writer_emit_writeout (MonoImageWriter *acfg)
1073 ElfProgHeader progh [4];
1074 ElfSectHeader secth [SECT_NUM];
1080 ElfStrTable str_table = {NULL, NULL};
1081 ElfStrTable sh_str_table = {NULL, NULL};
1082 ElfStrTable dyn_str_table = {NULL, NULL};
1083 BinSection* all_sections [32];
1084 BinSection* sections [SECT_NUM];
1087 ElfDynamic dynamic [14];
1089 int i, num_sections, file_offset, virt_offset, size, num_symtab;
1094 /* Section headers */
1095 memset (§h, 0, sizeof (secth));
1096 memset (&dynamic, 0, sizeof (dynamic));
1097 memset (&header, 0, sizeof (header));
1099 for (i = 1; i < SECT_NUM; ++i) {
1100 secth [i].sh_name = str_table_add (&sh_str_table, section_info [i].name);
1101 secth [i].sh_type = section_info [i].type;
1102 secth [i].sh_addralign = section_info [i].align;
1103 secth [i].sh_flags = section_info [i].flags;
1104 secth [i].sh_entsize = section_info [i].esize;
1106 secth [SECT_DYNSYM].sh_info = SIZEOF_VOID_P == 4 ? 4 : 2;
1107 secth [SECT_SYMTAB].sh_info = SIZEOF_VOID_P == 4 ? 20 : 17;
1108 secth [SECT_HASH].sh_link = SECT_DYNSYM;
1109 secth [SECT_DYNSYM].sh_link = SECT_DYNSTR;
1110 secth [SECT_REL_DYN].sh_link = SECT_DYNSYM;
1111 secth [SECT_RELA_DYN].sh_link = SECT_DYNSYM;
1112 secth [SECT_DYNAMIC].sh_link = SECT_DYNSTR;
1113 secth [SECT_SYMTAB].sh_link = SECT_STRTAB;
1115 num_sections = collect_sections (acfg, secth, all_sections, 16);
1116 hash = build_hash (acfg, num_sections, &dyn_str_table);
1117 num_symtab = hash [1]; /* FIXME */
1119 g_print ("num_sections: %d\n", num_sections);
1120 g_print ("dynsym: %d, dynstr size: %d\n", hash [1], (int)dyn_str_table.data->len);
1121 for (i = 0; i < num_sections; ++i) {
1122 g_print ("section %s, size: %d, %x\n", all_sections [i]->name, all_sections [i]->cur_offset, all_sections [i]->cur_offset);
1125 /* Associate the bin sections with the ELF sections */
1126 memset (sections, 0, sizeof (sections));
1127 for (i = 0; i < num_sections; ++i) {
1128 BinSection *sect = all_sections [i];
1131 for (j = 0; j < SECT_NUM; ++j) {
1132 if (strcmp (sect->name, section_info [j].name) == 0) {
1138 sections [all_sections [i]->shidx] = sect;
1141 /* at this point we know where in the file the first segment sections go */
1142 dynsym = collect_syms (acfg, hash, &dyn_str_table, NULL, NULL);
1143 num_local_syms = hash [1];
1144 symtab = collect_syms (acfg, NULL, &str_table, secth, &num_local_syms);
1146 file_offset = virt_offset = sizeof (header) + sizeof (progh);
1147 secth [SECT_HASH].sh_addr = secth [SECT_HASH].sh_offset = file_offset;
1148 size = sizeof (int) * (2 + hash [0] + hash [1]);
1149 virt_offset = (file_offset += size);
1150 secth [SECT_HASH].sh_size = size;
1151 secth [SECT_DYNSYM].sh_addr = secth [SECT_DYNSYM].sh_offset = file_offset;
1152 size = sizeof (ElfSymbol) * hash [1];
1153 virt_offset = (file_offset += size);
1154 secth [SECT_DYNSYM].sh_size = size;
1155 secth [SECT_DYNSTR].sh_addr = secth [SECT_DYNSTR].sh_offset = file_offset;
1156 size = dyn_str_table.data->len;
1157 virt_offset = (file_offset += size);
1158 secth [SECT_DYNSTR].sh_size = size;
1160 file_offset &= ~(4-1);
1161 secth [SECT_REL_DYN].sh_addr = secth [SECT_REL_DYN].sh_offset = file_offset;
1162 #ifndef USE_ELF_RELA
1163 size = sizeof (ElfReloc) * acfg->num_relocs;
1167 virt_offset = (file_offset += size);
1168 secth [SECT_REL_DYN].sh_size = size;
1169 secth [SECT_RELA_DYN].sh_addr = secth [SECT_RELA_DYN].sh_offset = file_offset;
1171 size = sizeof (ElfRelocA) * acfg->num_relocs;
1175 virt_offset = (file_offset += size);
1176 secth [SECT_RELA_DYN].sh_size = size;
1178 file_offset = ALIGN_TO (file_offset, secth [SECT_TEXT].sh_addralign);
1179 virt_offset = file_offset;
1180 secth [SECT_TEXT].sh_addr = secth [SECT_TEXT].sh_offset = file_offset;
1181 if (sections [SECT_TEXT]) {
1182 if (sections [SECT_TEXT]->has_addr) {
1183 secth [SECT_TEXT].sh_addr = sections [SECT_TEXT]->addr;
1184 secth [SECT_TEXT].sh_flags &= ~SHF_ALLOC;
1186 size = sections [SECT_TEXT]->cur_offset;
1187 secth [SECT_TEXT].sh_size = size;
1188 file_offset += size;
1191 file_offset = ALIGN_TO (file_offset, secth [SECT_RODATA].sh_addralign);
1192 virt_offset = file_offset;
1193 secth [SECT_RODATA].sh_addr = virt_offset;
1194 secth [SECT_RODATA].sh_offset = file_offset;
1195 if (sections [SECT_RODATA]) {
1196 size = sections [SECT_RODATA]->cur_offset;
1197 secth [SECT_RODATA].sh_size = size;
1198 file_offset += size;
1199 virt_offset += size;
1202 file_offset = ALIGN_TO (file_offset, secth [SECT_DYNAMIC].sh_addralign);
1203 virt_offset = file_offset;
1205 /* .dynamic, .got.plt, .data, .bss here */
1206 /* Have to increase the virt offset since these go to a separate segment */
1207 virt_offset += PAGESIZE;
1208 secth [SECT_DYNAMIC].sh_addr = virt_offset;
1209 secth [SECT_DYNAMIC].sh_offset = file_offset;
1210 size = sizeof (dynamic);
1211 secth [SECT_DYNAMIC].sh_size = size;
1212 file_offset += size;
1213 virt_offset += size;
1215 file_offset = ALIGN_TO (file_offset, secth [SECT_GOT_PLT].sh_addralign);
1216 virt_offset = ALIGN_TO (virt_offset, secth [SECT_GOT_PLT].sh_addralign);
1217 secth [SECT_GOT_PLT].sh_addr = virt_offset;
1218 secth [SECT_GOT_PLT].sh_offset = file_offset;
1219 size = 3 * SIZEOF_VOID_P;
1220 secth [SECT_GOT_PLT].sh_size = size;
1221 file_offset += size;
1222 virt_offset += size;
1224 file_offset = ALIGN_TO (file_offset, secth [SECT_DATA].sh_addralign);
1225 virt_offset = ALIGN_TO (virt_offset, secth [SECT_DATA].sh_addralign);
1226 secth [SECT_DATA].sh_addr = virt_offset;
1227 secth [SECT_DATA].sh_offset = file_offset;
1228 if (sections [SECT_DATA]) {
1229 size = sections [SECT_DATA]->cur_offset;
1230 secth [SECT_DATA].sh_size = size;
1231 file_offset += size;
1232 virt_offset += size;
1235 file_offset = ALIGN_TO (file_offset, secth [SECT_BSS].sh_addralign);
1236 virt_offset = ALIGN_TO (virt_offset, secth [SECT_BSS].sh_addralign);
1237 secth [SECT_BSS].sh_addr = virt_offset;
1238 secth [SECT_BSS].sh_offset = file_offset;
1239 if (sections [SECT_BSS]) {
1240 size = sections [SECT_BSS]->cur_offset;
1241 secth [SECT_BSS].sh_size = size;
1244 /* virtual doesn't matter anymore */
1245 file_offset = ALIGN_TO (file_offset, secth [SECT_DEBUG_FRAME].sh_addralign);
1246 secth [SECT_DEBUG_FRAME].sh_offset = file_offset;
1247 if (sections [SECT_DEBUG_FRAME])
1248 size = sections [SECT_DEBUG_FRAME]->cur_offset;
1251 secth [SECT_DEBUG_FRAME].sh_size = size;
1252 file_offset += size;
1254 secth [SECT_DEBUG_INFO].sh_offset = file_offset;
1255 if (sections [SECT_DEBUG_INFO])
1256 size = sections [SECT_DEBUG_INFO]->cur_offset;
1259 secth [SECT_DEBUG_INFO].sh_size = size;
1260 file_offset += size;
1262 secth [SECT_DEBUG_ABBREV].sh_offset = file_offset;
1263 if (sections [SECT_DEBUG_ABBREV])
1264 size = sections [SECT_DEBUG_ABBREV]->cur_offset;
1267 secth [SECT_DEBUG_ABBREV].sh_size = size;
1268 file_offset += size;
1270 secth [SECT_DEBUG_LINE].sh_offset = file_offset;
1271 if (sections [SECT_DEBUG_LINE])
1272 size = sections [SECT_DEBUG_LINE]->cur_offset;
1275 secth [SECT_DEBUG_LINE].sh_size = size;
1276 file_offset += size;
1278 secth [SECT_DEBUG_LOC].sh_offset = file_offset;
1279 if (sections [SECT_DEBUG_LOC])
1280 size = sections [SECT_DEBUG_LOC]->cur_offset;
1283 secth [SECT_DEBUG_LOC].sh_size = size;
1284 file_offset += size;
1286 file_offset = ALIGN_TO (file_offset, secth [SECT_SHSTRTAB].sh_addralign);
1287 secth [SECT_SHSTRTAB].sh_offset = file_offset;
1288 size = sh_str_table.data->len;
1289 secth [SECT_SHSTRTAB].sh_size = size;
1290 file_offset += size;
1292 file_offset = ALIGN_TO (file_offset, secth [SECT_SYMTAB].sh_addralign);
1293 secth [SECT_SYMTAB].sh_offset = file_offset;
1294 size = sizeof (ElfSymbol) * num_local_syms;
1295 secth [SECT_SYMTAB].sh_size = size;
1296 file_offset += size;
1298 file_offset = ALIGN_TO (file_offset, secth [SECT_STRTAB].sh_addralign);
1299 secth [SECT_STRTAB].sh_offset = file_offset;
1300 size = str_table.data->len;
1301 secth [SECT_STRTAB].sh_size = size;
1302 file_offset += size;
1304 for (i = 1; i < SECT_NUM; ++i) {
1305 if (section_info [i].esize != 0)
1306 g_assert (secth [i].sh_size % section_info [i].esize == 0);
1310 file_offset &= ~(4-1);
1312 header.e_ident [EI_MAG0] = ELFMAG0;
1313 header.e_ident [EI_MAG1] = ELFMAG1;
1314 header.e_ident [EI_MAG2] = ELFMAG2;
1315 header.e_ident [EI_MAG3] = ELFMAG3;
1316 header.e_ident [EI_CLASS] = SIZEOF_VOID_P == 4 ? ELFCLASS32 : ELFCLASS64;
1317 header.e_ident [EI_DATA] = ELFDATA2LSB;
1318 header.e_ident [EI_VERSION] = EV_CURRENT;
1319 header.e_ident [EI_OSABI] = ELFOSABI_NONE;
1320 header.e_ident [EI_ABIVERSION] = 0;
1321 for (i = EI_PAD; i < EI_NIDENT; ++i)
1322 header.e_ident [i] = 0;
1324 header.e_type = ET_DYN;
1325 #if defined(TARGET_X86)
1326 header.e_machine = EM_386;
1327 #elif defined(TARGET_AMD64)
1328 header.e_machine = EM_X86_64;
1329 #elif defined(TARGET_ARM)
1330 header.e_machine = EM_ARM;
1332 g_assert_not_reached ();
1334 header.e_version = 1;
1336 header.e_phoff = sizeof (header);
1337 header.e_ehsize = sizeof (header);
1338 header.e_phentsize = sizeof (ElfProgHeader);
1340 header.e_entry = secth [SECT_TEXT].sh_addr;
1341 header.e_shstrndx = SECT_SHSTRTAB;
1342 header.e_shentsize = sizeof (ElfSectHeader);
1343 header.e_shnum = SECT_NUM;
1344 header.e_shoff = file_offset;
1348 dynamic [i].d_tag = DT_HASH;
1349 dynamic [i].d_un.d_val = secth [SECT_HASH].sh_offset;
1351 dynamic [i].d_tag = DT_STRTAB;
1352 dynamic [i].d_un.d_val = secth [SECT_DYNSTR].sh_offset;
1354 dynamic [i].d_tag = DT_SYMTAB;
1355 dynamic [i].d_un.d_val = secth [SECT_DYNSYM].sh_offset;
1357 dynamic [i].d_tag = DT_STRSZ;
1358 dynamic [i].d_un.d_val = dyn_str_table.data->len;
1360 dynamic [i].d_tag = DT_SYMENT;
1361 dynamic [i].d_un.d_val = sizeof (ElfSymbol);
1364 dynamic [i].d_tag = DT_RELA;
1365 dynamic [i].d_un.d_val = secth [SECT_RELA_DYN].sh_offset;
1367 dynamic [i].d_tag = DT_RELASZ;
1368 dynamic [i].d_un.d_val = secth [SECT_RELA_DYN].sh_size;
1370 dynamic [i].d_tag = DT_RELAENT;
1371 dynamic [i].d_un.d_val = sizeof (ElfRelocA);
1374 dynamic [i].d_tag = DT_REL;
1375 dynamic [i].d_un.d_val = secth [SECT_REL_DYN].sh_offset;
1377 dynamic [i].d_tag = DT_RELSZ;
1378 dynamic [i].d_un.d_val = secth [SECT_REL_DYN].sh_size;
1380 dynamic [i].d_tag = DT_RELENT;
1381 dynamic [i].d_un.d_val = sizeof (ElfReloc);
1384 dynamic [i].d_tag = DT_RELCOUNT;
1385 dynamic [i].d_un.d_val = acfg->num_relocs;
1388 /* Program header */
1389 memset (&progh, 0, sizeof (progh));
1390 progh [0].p_type = PT_LOAD;
1391 progh [0].p_filesz = progh [0].p_memsz = secth [SECT_DYNAMIC].sh_offset;
1392 progh [0].p_align = 4096;
1393 progh [0].p_flags = 5;
1395 progh [1].p_type = PT_LOAD;
1396 progh [1].p_offset = secth [SECT_DYNAMIC].sh_offset;
1397 progh [1].p_vaddr = progh [1].p_paddr = secth [SECT_DYNAMIC].sh_addr;
1398 progh [1].p_filesz = secth [SECT_BSS].sh_offset - secth [SECT_DYNAMIC].sh_offset;
1399 progh [1].p_memsz = secth [SECT_BSS].sh_addr + secth [SECT_BSS].sh_size - secth [SECT_DYNAMIC].sh_addr;
1400 progh [1].p_align = 4096;
1401 progh [1].p_flags = 6;
1403 progh [2].p_type = PT_DYNAMIC;
1404 progh [2].p_offset = secth [SECT_DYNAMIC].sh_offset;
1405 progh [2].p_vaddr = progh [2].p_paddr = secth [SECT_DYNAMIC].sh_addr;
1406 progh [2].p_filesz = progh [2].p_memsz = secth [SECT_DYNAMIC].sh_size;
1407 progh [2].p_align = SIZEOF_VOID_P;
1408 progh [2].p_flags = 6;
1410 progh [3].p_type = PT_GNU_STACK;
1411 progh [3].p_offset = secth [SECT_DYNAMIC].sh_offset;
1412 progh [3].p_vaddr = progh [3].p_paddr = secth [SECT_DYNAMIC].sh_addr;
1413 progh [3].p_filesz = progh [3].p_memsz = secth [SECT_DYNAMIC].sh_size;
1414 progh [3].p_align = SIZEOF_VOID_P;
1415 progh [3].p_flags = 6;
1417 /* Compute the addresses of the bin sections, so relocation can be done */
1418 for (i = 0; i < SECT_NUM; ++i) {
1420 sections [i]->file_offset = secth [i].sh_offset;
1421 sections [i]->virt_offset = secth [i].sh_addr;
1425 reloc_symbols (acfg, dynsym, secth, &dyn_str_table, TRUE);
1426 reloc_symbols (acfg, symtab, secth, &str_table, FALSE);
1427 relocs = resolve_relocations (acfg);
1430 acfg->out_buf_size = file_offset + sizeof (secth);
1431 acfg->out_buf = g_malloc (acfg->out_buf_size);
1434 bin_writer_fwrite (acfg, &header, sizeof (header), 1);
1435 bin_writer_fwrite (acfg, &progh, sizeof (progh), 1);
1436 bin_writer_fwrite (acfg, hash, sizeof (int) * (hash [0] + hash [1] + 2), 1);
1437 bin_writer_fwrite (acfg, dynsym, sizeof (ElfSymbol) * hash [1], 1);
1438 bin_writer_fwrite (acfg, dyn_str_table.data->str, dyn_str_table.data->len, 1);
1440 bin_writer_fseek (acfg, secth [SECT_REL_DYN].sh_offset);
1441 bin_writer_fwrite (acfg, relocs, sizeof (ElfReloc), acfg->num_relocs);
1444 bin_writer_fseek (acfg, secth [SECT_RELA_DYN].sh_offset);
1445 bin_writer_fwrite (acfg, relocs, secth [SECT_RELA_DYN].sh_size, 1);
1448 if (sections [SECT_TEXT]) {
1449 bin_writer_fseek (acfg, secth [SECT_TEXT].sh_offset);
1450 bin_writer_fwrite (acfg, sections [SECT_TEXT]->data, sections [SECT_TEXT]->cur_offset, 1);
1453 if (sections [SECT_RODATA]) {
1454 bin_writer_fseek (acfg, secth [SECT_RODATA].sh_offset);
1455 bin_writer_fwrite (acfg, sections [SECT_RODATA]->data, sections [SECT_RODATA]->cur_offset, 1);
1458 bin_writer_fseek (acfg, secth [SECT_DYNAMIC].sh_offset);
1459 bin_writer_fwrite (acfg, dynamic, sizeof (dynamic), 1);
1462 size = secth [SECT_DYNAMIC].sh_addr;
1463 bin_writer_fseek (acfg, secth [SECT_GOT_PLT].sh_offset);
1464 bin_writer_fwrite (acfg, &size, sizeof (size), 1);
1466 /* normal sections */
1467 for (i = 0; i < sizeof (normal_sections) / sizeof (normal_sections [0]); ++i) {
1468 int sect = normal_sections [i];
1470 if (sections [sect]) {
1471 bin_writer_fseek (acfg, secth [sect].sh_offset);
1472 bin_writer_fwrite (acfg, sections [sect]->data, sections [sect]->cur_offset, 1);
1476 bin_writer_fseek (acfg, secth [SECT_SHSTRTAB].sh_offset);
1477 bin_writer_fwrite (acfg, sh_str_table.data->str, sh_str_table.data->len, 1);
1478 bin_writer_fseek (acfg, secth [SECT_SYMTAB].sh_offset);
1479 bin_writer_fwrite (acfg, symtab, sizeof (ElfSymbol) * num_local_syms, 1);
1480 bin_writer_fseek (acfg, secth [SECT_STRTAB].sh_offset);
1481 bin_writer_fwrite (acfg, str_table.data->str, str_table.data->len, 1);
1482 /*g_print ("file_offset %d vs %d\n", file_offset, ftell (file));*/
1483 /*g_assert (file_offset >= ftell (file));*/
1484 bin_writer_fseek (acfg, file_offset);
1485 bin_writer_fwrite (acfg, §h, sizeof (secth), 1);
1493 #endif /* USE_ELF_WRITER */
1495 #endif /* USE_BIN_WRITER */
1500 asm_writer_emit_start (MonoImageWriter *acfg)
1505 asm_writer_emit_writeout (MonoImageWriter *acfg)
1513 asm_writer_emit_unset_mode (MonoImageWriter *acfg)
1515 if (acfg->mode == EMIT_NONE)
1517 fprintf (acfg->fp, "\n");
1518 acfg->mode = EMIT_NONE;
1522 asm_writer_emit_section_change (MonoImageWriter *acfg, const char *section_name, int subsection_index)
1524 asm_writer_emit_unset_mode (acfg);
1525 #if defined(TARGET_ASM_APPLE)
1526 if (strcmp(section_name, ".bss") == 0)
1527 fprintf (acfg->fp, "%s\n", ".data");
1528 else if (strstr (section_name, ".debug") == section_name) {
1529 //g_assert (subsection_index == 0);
1530 fprintf (acfg->fp, ".section __DWARF, __%s,regular,debug\n", section_name + 1);
1532 fprintf (acfg->fp, "%s\n", section_name);
1533 #elif defined(TARGET_ARM) || defined(TARGET_POWERPC)
1534 /* ARM gas doesn't seem to like subsections of .bss */
1535 if (!strcmp (section_name, ".text") || !strcmp (section_name, ".data")) {
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);
1541 #elif defined(HOST_WIN32)
1542 fprintf (acfg->fp, ".section %s\n", section_name);
1544 if (!strcmp (section_name, ".text") || !strcmp (section_name, ".data") || !strcmp (section_name, ".bss")) {
1545 fprintf (acfg->fp, "%s %d\n", section_name, subsection_index);
1547 fprintf (acfg->fp, ".section \"%s\"\n", section_name);
1548 fprintf (acfg->fp, ".subsection %d\n", subsection_index);
1554 const char *get_label (const char *s)
1556 #ifdef TARGET_ASM_APPLE
1557 if (s [0] == '.' && s [1] == 'L')
1558 /* apple uses "L" instead of ".L" to mark temporary labels */
1565 asm_writer_emit_symbol_type (MonoImageWriter *acfg, const char *name, gboolean func)
1574 asm_writer_emit_unset_mode (acfg);
1575 #if defined(TARGET_ASM_APPLE)
1577 #elif defined(TARGET_ARM)
1578 fprintf (acfg->fp, "\t.type %s,#%s\n", name, stype);
1580 fprintf (acfg->fp, "\t.type %s,@%s\n", name, stype);
1585 asm_writer_emit_global (MonoImageWriter *acfg, const char *name, gboolean func)
1587 asm_writer_emit_unset_mode (acfg);
1588 #if ((defined(__ppc__) || defined(TARGET_X86)) && defined(TARGET_ASM_APPLE)) || (defined(HOST_WIN32) && !defined(MONO_CROSS_COMPILE))
1589 // mach-o always uses a '_' prefix.
1590 fprintf (acfg->fp, "\t.globl _%s\n", name);
1592 fprintf (acfg->fp, "\t.globl %s\n", name);
1595 asm_writer_emit_symbol_type (acfg, name, func);
1599 asm_writer_emit_local_symbol (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean func)
1601 asm_writer_emit_unset_mode (acfg);
1603 #ifndef TARGET_ASM_APPLE
1604 fprintf (acfg->fp, "\t.local %s\n", name);
1607 asm_writer_emit_symbol_type (acfg, name, func);
1611 asm_writer_emit_symbol_size (MonoImageWriter *acfg, const char *name, const char *end_label)
1613 asm_writer_emit_unset_mode (acfg);
1615 #ifndef TARGET_ASM_APPLE
1616 fprintf (acfg->fp, "\t.size %s,%s-%s\n", name, end_label, name);
1621 asm_writer_emit_label (MonoImageWriter *acfg, const char *name)
1623 asm_writer_emit_unset_mode (acfg);
1624 #if (defined(TARGET_X86) && defined(TARGET_ASM_APPLE))
1625 name = get_label(name);
1626 fprintf (acfg->fp, "%s:\n", name);
1627 if (name[0] != 'L' && name[0] != '_')
1628 fprintf (acfg->fp, "_%s:\n", name);
1630 #elif (defined(HOST_WIN32) && (defined(TARGET_X86) || defined(TARGET_AMD64))) || (defined(TARGET_X86) && defined(TARGET_ASM_APPLE))
1631 fprintf (acfg->fp, "_%s:\n", name);
1632 #if defined(HOST_WIN32)
1633 /* Emit a normal label too */
1634 fprintf (acfg->fp, "%s:\n", name);
1637 fprintf (acfg->fp, "%s:\n", get_label (name));
1643 asm_writer_emit_string (MonoImageWriter *acfg, const char *value)
1645 asm_writer_emit_unset_mode (acfg);
1646 fprintf (acfg->fp, "\t%s \"%s\"\n", AS_STRING_DIRECTIVE, value);
1650 asm_writer_emit_line (MonoImageWriter *acfg)
1652 asm_writer_emit_unset_mode (acfg);
1653 fprintf (acfg->fp, "\n");
1657 asm_writer_emit_alignment (MonoImageWriter *acfg, int size)
1659 asm_writer_emit_unset_mode (acfg);
1660 #if defined(TARGET_ARM)
1661 fprintf (acfg->fp, "\t.align %d\n", ilog2 (size));
1662 #elif defined(__ppc__) && defined(TARGET_ASM_APPLE)
1663 // the mach-o assembler specifies alignments as powers of 2.
1664 fprintf (acfg->fp, "\t.align %d\t; ilog2\n", ilog2(size));
1665 #elif defined(TARGET_ASM_GAS)
1666 fprintf (acfg->fp, "\t.balign %d\n", size);
1667 #elif defined(TARGET_ASM_APPLE)
1668 fprintf (acfg->fp, "\t.align %d\n", ilog2 (size));
1670 fprintf (acfg->fp, "\t.align %d\n", size);
1674 #ifdef __native_client_codegen__
1676 asm_writer_emit_nacl_call_alignment (MonoImageWriter *acfg) {
1677 int padding = kNaClAlignment - kNaClLengthOfCallImm;
1678 guint8 padc = '\x90';
1680 fprintf (acfg->fp, "\n\t.align %d", kNaClAlignment);
1681 while (padding > 0) {
1682 fprintf (acfg->fp, "\n\t.byte %d", padc);
1686 #endif /* __native_client_codegen__ */
1689 asm_writer_emit_pointer_unaligned (MonoImageWriter *acfg, const char *target)
1691 asm_writer_emit_unset_mode (acfg);
1692 fprintf (acfg->fp, "\t%s %s\n", AS_POINTER_DIRECTIVE, target ? target : "0");
1696 asm_writer_emit_pointer (MonoImageWriter *acfg, const char *target)
1698 asm_writer_emit_unset_mode (acfg);
1699 asm_writer_emit_alignment (acfg, sizeof (gpointer));
1700 asm_writer_emit_pointer_unaligned (acfg, target);
1703 static char *byte_to_str;
1706 asm_writer_emit_bytes (MonoImageWriter *acfg, const guint8* buf, int size)
1709 if (acfg->mode != EMIT_BYTE) {
1710 acfg->mode = EMIT_BYTE;
1711 acfg->col_count = 0;
1714 if (byte_to_str == NULL) {
1715 byte_to_str = g_new0 (char, 256 * 8);
1716 for (i = 0; i < 256; ++i) {
1717 sprintf (byte_to_str + (i * 8), ",%d", i);
1721 for (i = 0; i < size; ++i, ++acfg->col_count) {
1722 if ((acfg->col_count % 32) == 0)
1723 fprintf (acfg->fp, "\n\t.byte %d", buf [i]);
1725 fputs (byte_to_str + (buf [i] * 8), acfg->fp);
1730 asm_writer_emit_int16 (MonoImageWriter *acfg, int value)
1732 if (acfg->mode != EMIT_WORD) {
1733 acfg->mode = EMIT_WORD;
1734 acfg->col_count = 0;
1736 if ((acfg->col_count++ % 8) == 0)
1737 fprintf (acfg->fp, "\n\t%s ", AS_INT16_DIRECTIVE);
1739 fprintf (acfg->fp, ", ");
1740 fprintf (acfg->fp, "%d", value);
1744 asm_writer_emit_int32 (MonoImageWriter *acfg, int value)
1746 if (acfg->mode != EMIT_LONG) {
1747 acfg->mode = EMIT_LONG;
1748 acfg->col_count = 0;
1750 if ((acfg->col_count++ % 8) == 0)
1751 fprintf (acfg->fp, "\n\t%s ", AS_INT32_DIRECTIVE);
1753 fprintf (acfg->fp, ",");
1754 fprintf (acfg->fp, "%d", value);
1758 asm_writer_emit_symbol_diff (MonoImageWriter *acfg, const char *end, const char* start, int offset)
1760 #ifdef TARGET_ASM_APPLE
1761 //char symbol [128];
1764 if (acfg->mode != EMIT_LONG) {
1765 acfg->mode = EMIT_LONG;
1766 acfg->col_count = 0;
1769 // FIXME: This doesn't seem to work on the iphone
1771 //#ifdef TARGET_ASM_APPLE
1772 /* The apple assembler needs a separate symbol to be able to handle complex expressions */
1773 sprintf (symbol, "LTMP_SYM%d", acfg->label_gen);
1774 start = get_label (start);
1775 end = get_label (end);
1778 fprintf (acfg->fp, "\n%s=%s - %s + %d", symbol, end, start, offset);
1779 else if (offset < 0)
1780 fprintf (acfg->fp, "\n%s=%s - %s %d", symbol, end, start, offset);
1782 fprintf (acfg->fp, "\n%s=%s - %s", symbol, end, start);
1784 fprintf (acfg->fp, "\n\t%s ", AS_INT32_DIRECTIVE);
1785 fprintf (acfg->fp, "%s", symbol);
1787 start = get_label (start);
1788 end = get_label (end);
1790 if (offset == 0 && strcmp (start, ".") != 0) {
1792 sprintf (symbol, "%sDIFF_SYM%d", AS_TEMP_LABEL_PREFIX, acfg->label_gen);
1794 fprintf (acfg->fp, "\n%s=%s - %s", symbol, end, start);
1795 fprintf (acfg->fp, "\n\t%s ", AS_INT32_DIRECTIVE);
1796 fprintf (acfg->fp, "%s", symbol);
1800 if ((acfg->col_count++ % 8) == 0)
1801 fprintf (acfg->fp, "\n\t%s ", AS_INT32_DIRECTIVE);
1803 fprintf (acfg->fp, ",");
1805 fprintf (acfg->fp, "%s - %s + %d", end, start, offset);
1806 else if (offset < 0)
1807 fprintf (acfg->fp, "%s - %s %d", end, start, offset);
1809 fprintf (acfg->fp, "%s - %s", end, start);
1814 asm_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
1816 asm_writer_emit_unset_mode (acfg);
1817 fprintf (acfg->fp, "\t%s %d\n", AS_SKIP_DIRECTIVE, num);
1820 /* EMIT FUNCTIONS */
1823 img_writer_emit_start (MonoImageWriter *acfg)
1825 #ifdef USE_BIN_WRITER
1826 if (acfg->use_bin_writer)
1827 bin_writer_emit_start (acfg);
1829 asm_writer_emit_start (acfg);
1831 asm_writer_emit_start (acfg);
1836 img_writer_emit_section_change (MonoImageWriter *acfg, const char *section_name, int subsection_index)
1838 #ifdef USE_BIN_WRITER
1839 if (acfg->use_bin_writer)
1840 bin_writer_emit_section_change (acfg, section_name, subsection_index);
1842 asm_writer_emit_section_change (acfg, section_name, subsection_index);
1844 asm_writer_emit_section_change (acfg, section_name, subsection_index);
1847 acfg->current_section = section_name;
1848 acfg->current_subsection = subsection_index;
1852 img_writer_emit_push_section (MonoImageWriter *acfg, const char *section_name, int subsection)
1854 g_assert (acfg->stack_pos < 16 - 1);
1855 acfg->section_stack [acfg->stack_pos] = acfg->current_section;
1856 acfg->subsection_stack [acfg->stack_pos] = acfg->current_subsection;
1859 img_writer_emit_section_change (acfg, section_name, subsection);
1863 img_writer_emit_pop_section (MonoImageWriter *acfg)
1865 g_assert (acfg->stack_pos > 0);
1867 img_writer_emit_section_change (acfg, acfg->section_stack [acfg->stack_pos], acfg->subsection_stack [acfg->stack_pos]);
1871 img_writer_set_section_addr (MonoImageWriter *acfg, guint64 addr)
1873 #ifdef USE_BIN_WRITER
1874 if (!acfg->use_bin_writer)
1877 bin_writer_set_section_addr (acfg, addr);
1884 img_writer_emit_global (MonoImageWriter *acfg, const char *name, gboolean func)
1886 #ifdef USE_BIN_WRITER
1887 if (acfg->use_bin_writer)
1888 bin_writer_emit_global (acfg, name, func);
1890 asm_writer_emit_global (acfg, name, func);
1892 asm_writer_emit_global (acfg, name, func);
1897 img_writer_emit_local_symbol (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean func)
1899 #ifdef USE_BIN_WRITER
1900 if (acfg->use_bin_writer)
1901 bin_writer_emit_local_symbol (acfg, name, end_label, func);
1903 asm_writer_emit_local_symbol (acfg, name, end_label, func);
1905 asm_writer_emit_local_symbol (acfg, name, end_label, func);
1910 img_writer_emit_symbol_size (MonoImageWriter *acfg, const char *name, const char *end_label)
1912 if (!acfg->use_bin_writer)
1913 asm_writer_emit_symbol_size (acfg, name, end_label);
1917 img_writer_emit_label (MonoImageWriter *acfg, const char *name)
1919 #ifdef USE_BIN_WRITER
1920 if (acfg->use_bin_writer)
1921 bin_writer_emit_label (acfg, name);
1923 asm_writer_emit_label (acfg, name);
1925 asm_writer_emit_label (acfg, name);
1930 img_writer_emit_bytes (MonoImageWriter *acfg, const guint8* buf, int size)
1932 #ifdef USE_BIN_WRITER
1933 if (acfg->use_bin_writer)
1934 bin_writer_emit_bytes (acfg, buf, size);
1936 asm_writer_emit_bytes (acfg, buf, size);
1938 asm_writer_emit_bytes (acfg, buf, size);
1943 img_writer_emit_string (MonoImageWriter *acfg, const char *value)
1945 #ifdef USE_BIN_WRITER
1946 if (acfg->use_bin_writer)
1947 bin_writer_emit_string (acfg, value);
1949 asm_writer_emit_string (acfg, value);
1951 asm_writer_emit_string (acfg, value);
1956 img_writer_emit_line (MonoImageWriter *acfg)
1958 #ifdef USE_BIN_WRITER
1959 if (acfg->use_bin_writer)
1960 bin_writer_emit_line (acfg);
1962 asm_writer_emit_line (acfg);
1964 asm_writer_emit_line (acfg);
1969 img_writer_emit_alignment (MonoImageWriter *acfg, int size)
1971 #ifdef USE_BIN_WRITER
1972 if (acfg->use_bin_writer)
1973 bin_writer_emit_alignment (acfg, size);
1975 asm_writer_emit_alignment (acfg, size);
1977 asm_writer_emit_alignment (acfg, size);
1981 #ifdef __native_client_codegen__
1983 img_writer_emit_nacl_call_alignment (MonoImageWriter *acfg) {
1984 #ifdef USE_BIN_WRITER
1985 if (acfg->use_bin_writer)
1986 bin_writer_emit_nacl_call_alignment (acfg);
1988 asm_writer_emit_nacl_call_alignment (acfg);
1990 g_assert_not_reached();
1993 #endif /* __native_client_codegen__ */
1996 img_writer_emit_pointer_unaligned (MonoImageWriter *acfg, const char *target)
1998 #ifdef USE_BIN_WRITER
1999 if (acfg->use_bin_writer)
2000 bin_writer_emit_pointer_unaligned (acfg, target);
2002 asm_writer_emit_pointer_unaligned (acfg, target);
2004 asm_writer_emit_pointer_unaligned (acfg, target);
2009 img_writer_emit_pointer (MonoImageWriter *acfg, const char *target)
2011 #ifdef USE_BIN_WRITER
2012 if (acfg->use_bin_writer)
2013 bin_writer_emit_pointer (acfg, target);
2015 asm_writer_emit_pointer (acfg, target);
2017 asm_writer_emit_pointer (acfg, target);
2022 img_writer_emit_int16 (MonoImageWriter *acfg, int value)
2024 #ifdef USE_BIN_WRITER
2025 if (acfg->use_bin_writer)
2026 bin_writer_emit_int16 (acfg, value);
2028 asm_writer_emit_int16 (acfg, value);
2030 asm_writer_emit_int16 (acfg, value);
2035 img_writer_emit_int32 (MonoImageWriter *acfg, int value)
2037 #ifdef USE_BIN_WRITER
2038 if (acfg->use_bin_writer)
2039 bin_writer_emit_int32 (acfg, value);
2041 asm_writer_emit_int32 (acfg, value);
2043 asm_writer_emit_int32 (acfg, value);
2048 img_writer_emit_symbol_diff (MonoImageWriter *acfg, const char *end, const char* start, int offset)
2050 #ifdef USE_BIN_WRITER
2051 if (acfg->use_bin_writer)
2052 bin_writer_emit_symbol_diff (acfg, end, start, offset);
2054 asm_writer_emit_symbol_diff (acfg, end, start, offset);
2056 asm_writer_emit_symbol_diff (acfg, end, start, offset);
2061 img_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
2063 #ifdef USE_BIN_WRITER
2064 if (acfg->use_bin_writer)
2065 bin_writer_emit_zero_bytes (acfg, num);
2067 asm_writer_emit_zero_bytes (acfg, num);
2069 asm_writer_emit_zero_bytes (acfg, num);
2074 img_writer_emit_writeout (MonoImageWriter *acfg)
2076 #ifdef USE_BIN_WRITER
2077 if (acfg->use_bin_writer)
2078 return bin_writer_emit_writeout (acfg);
2080 return asm_writer_emit_writeout (acfg);
2082 return asm_writer_emit_writeout (acfg);
2087 img_writer_emit_byte (MonoImageWriter *acfg, guint8 val)
2089 img_writer_emit_bytes (acfg, &val, 1);
2093 * Emit a relocation entry of type RELOC_TYPE against symbol SYMBOL at the current PC.
2094 * Do not advance PC.
2097 img_writer_emit_reloc (MonoImageWriter *acfg, int reloc_type, const char *symbol, int addend)
2099 /* This is only supported by the bin writer */
2100 #ifdef USE_BIN_WRITER
2101 if (acfg->use_bin_writer)
2102 bin_writer_emit_reloc (acfg, reloc_type, symbol, addend);
2104 g_assert_not_reached ();
2106 g_assert_not_reached ();
2111 * img_writer_emit_unset_mode:
2113 * Flush buffered data so it is safe to write to the output file from outside this
2114 * module. This is a nop for the binary writer.
2117 img_writer_emit_unset_mode (MonoImageWriter *acfg)
2119 if (!acfg->use_bin_writer)
2120 asm_writer_emit_unset_mode (acfg);
2124 * img_writer_get_output:
2126 * Return the output buffer of a binary writer emitting to memory. The returned memory
2127 * is from malloc, and it is owned by the caller.
2130 img_writer_get_output (MonoImageWriter *acfg, guint32 *size)
2132 #ifdef USE_BIN_WRITER
2135 g_assert (acfg->use_bin_writer);
2137 buf = acfg->out_buf;
2138 *size = acfg->out_buf_size;
2139 acfg->out_buf = NULL;
2142 g_assert_not_reached ();
2148 * Return whenever the binary writer is supported on this platform.
2151 bin_writer_supported (void)
2153 #ifdef USE_BIN_WRITER
2161 * img_writer_create:
2163 * Create an image writer writing to FP. If USE_BIN_WRITER is TRUE, FP can be NULL,
2164 * in this case the image writer will write to a memory buffer obtainable by calling
2165 * img_writer_get_output ().
2168 img_writer_create (FILE *fp, gboolean use_bin_writer)
2170 MonoImageWriter *w = g_new0 (MonoImageWriter, 1);
2172 #ifndef USE_BIN_WRITER
2173 g_assert (!use_bin_writer);
2176 if (!use_bin_writer)
2180 w->use_bin_writer = use_bin_writer;
2181 w->mempool = mono_mempool_new ();
2187 img_writer_destroy (MonoImageWriter *w)
2189 // FIXME: Free all the stuff
2190 mono_mempool_destroy (w->mempool);
2195 img_writer_subsections_supported (MonoImageWriter *acfg)
2197 #ifdef TARGET_ASM_APPLE
2198 return acfg->use_bin_writer;
2205 img_writer_get_fp (MonoImageWriter *acfg)
2211 img_writer_get_temp_label_prefix (MonoImageWriter *acfg)
2213 return AS_TEMP_LABEL_PREFIX;