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)
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)
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 memcpy (data, section->data, section->data_len);
317 g_free (section->data);
318 section->data = data;
319 section->data_len = new_size;
324 bin_writer_emit_bytes (MonoImageWriter *acfg, const guint8* buf, int size)
326 bin_writer_emit_ensure_buffer (acfg->cur_section, size);
327 memcpy (acfg->cur_section->data + acfg->cur_section->cur_offset, buf, size);
328 acfg->cur_section->cur_offset += size;
332 bin_writer_emit_string (MonoImageWriter *acfg, const char *value)
334 int size = strlen (value) + 1;
335 bin_writer_emit_bytes (acfg, (const guint8*)value, size);
339 bin_writer_emit_line (MonoImageWriter *acfg)
341 /* Nothing to do in binary writer */
345 bin_writer_emit_alignment (MonoImageWriter *acfg, int size)
347 int offset = acfg->cur_section->cur_offset;
349 offset += (size - 1);
350 offset &= ~(size - 1);
351 add = offset - acfg->cur_section->cur_offset;
353 bin_writer_emit_ensure_buffer (acfg->cur_section, add);
354 acfg->cur_section->cur_offset += add;
359 bin_writer_emit_pointer_unaligned (MonoImageWriter *acfg, const char *target)
365 g_assert_not_reached ();
366 reloc = g_new0 (BinReloc, 1);
367 reloc->val1 = g_strdup (target);
368 reloc->section = acfg->cur_section;
369 reloc->section_offset = acfg->cur_section->cur_offset;
370 reloc->next = acfg->relocations;
371 acfg->relocations = reloc;
372 if (strcmp (reloc->section->name, ".data") == 0) {
374 //g_print ("reloc: %s at %d\n", target, acfg->cur_section->cur_offset);
376 acfg->cur_section->cur_offset += sizeof (gpointer);
380 bin_writer_emit_pointer (MonoImageWriter *acfg, const char *target)
382 bin_writer_emit_alignment (acfg, sizeof (gpointer));
383 bin_writer_emit_pointer_unaligned (acfg, target);
387 bin_writer_emit_int16 (MonoImageWriter *acfg, int value)
390 bin_writer_emit_ensure_buffer (acfg->cur_section, 2);
391 data = acfg->cur_section->data + acfg->cur_section->cur_offset;
392 acfg->cur_section->cur_offset += 2;
393 /* FIXME: little endian */
395 data [1] = value >> 8;
399 bin_writer_emit_int32 (MonoImageWriter *acfg, int value)
402 bin_writer_emit_ensure_buffer (acfg->cur_section, 4);
403 data = acfg->cur_section->data + acfg->cur_section->cur_offset;
404 acfg->cur_section->cur_offset += 4;
405 /* FIXME: little endian */
407 data [1] = value >> 8;
408 data [2] = value >> 16;
409 data [3] = value >> 24;
413 create_reloc (MonoImageWriter *acfg, const char *end, const char* start, int offset)
416 reloc = mono_mempool_alloc0 (acfg->mempool, sizeof (BinReloc));
417 reloc->val1 = mono_mempool_strdup (acfg->mempool, end);
418 if (strcmp (start, ".") == 0) {
419 reloc->val2_section = acfg->cur_section;
420 reloc->val2_offset = acfg->cur_section->cur_offset;
422 reloc->val2 = mono_mempool_strdup (acfg->mempool, start);
424 reloc->offset = offset;
425 reloc->section = acfg->cur_section;
426 reloc->section_offset = acfg->cur_section->cur_offset;
427 reloc->next = acfg->relocations;
428 acfg->relocations = reloc;
433 bin_writer_emit_symbol_diff (MonoImageWriter *acfg, const char *end, const char* start, int offset)
435 create_reloc (acfg, end, start, offset);
436 acfg->cur_section->cur_offset += 4;
437 /*if (strcmp (reloc->section->name, ".data") == 0) {
439 g_print ("reloc: %s - %s + %d at %d\n", end, start, offset, acfg->cur_section->cur_offset - 4);
444 * Emit a relocation entry of type RELOC_TYPE against symbol SYMBOL at the current PC.
447 static G_GNUC_UNUSED void
448 bin_writer_emit_reloc (MonoImageWriter *acfg, int reloc_type, const char *symbol, int addend)
450 BinReloc *reloc = create_reloc (acfg, symbol, ".", addend);
451 reloc->reloc_type = reloc_type;
455 bin_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
457 bin_writer_emit_ensure_buffer (acfg->cur_section, num);
458 acfg->cur_section->cur_offset += num;
461 #ifdef USE_ELF_WRITER
486 #if SIZEOF_VOID_P == 4
488 typedef Elf32_Ehdr ElfHeader;
489 typedef Elf32_Shdr ElfSectHeader;
490 typedef Elf32_Phdr ElfProgHeader;
491 typedef Elf32_Sym ElfSymbol;
492 typedef Elf32_Rel ElfReloc;
493 typedef Elf32_Rela ElfRelocA;
494 typedef Elf32_Dyn ElfDynamic;
498 typedef Elf64_Ehdr ElfHeader;
499 typedef Elf64_Shdr ElfSectHeader;
500 typedef Elf64_Phdr ElfProgHeader;
501 typedef Elf64_Sym ElfSymbol;
502 typedef Elf64_Rel ElfReloc;
503 typedef Elf64_Rela ElfRelocA;
504 typedef Elf64_Dyn ElfDynamic;
516 static SectInfo section_info [] = {
518 {".hash", SHT_HASH, 4, 2, SIZEOF_VOID_P},
519 {".dynsym", SHT_DYNSYM, sizeof (ElfSymbol), 2, SIZEOF_VOID_P},
520 {".dynstr", SHT_STRTAB, 0, 2, 1},
521 {".rel.dyn", SHT_REL, sizeof (ElfReloc), 2, SIZEOF_VOID_P},
522 {".rela.dyn", SHT_RELA, sizeof (ElfRelocA), 2, SIZEOF_VOID_P},
523 {".text", SHT_PROGBITS, 0, 6, 4096},
524 {".dynamic", SHT_DYNAMIC, sizeof (ElfDynamic), 3, SIZEOF_VOID_P},
525 {".got.plt", SHT_PROGBITS, SIZEOF_VOID_P, 3, SIZEOF_VOID_P},
526 {".data", SHT_PROGBITS, 0, 3, 8},
527 {".bss", SHT_NOBITS, 0, 3, 8},
528 {".debug_frame", SHT_PROGBITS, 0, 0, 8},
529 {".debug_info", SHT_PROGBITS, 0, 0, 1},
530 {".debug_abbrev", SHT_PROGBITS, 0, 0, 1},
531 {".debug_line", SHT_PROGBITS, 0, 0, 1},
532 {".debug_loc", SHT_PROGBITS, 0, 0, 1},
533 {".shstrtab", SHT_STRTAB, 0, 0, 1},
534 {".symtab", SHT_SYMTAB, sizeof (ElfSymbol), 0, SIZEOF_VOID_P},
535 {".strtab", SHT_STRTAB, 0, 0, 1}
544 str_table_add (ElfStrTable *table, const char* value)
548 table->data = g_string_new_len ("", 1);
549 table->hash = g_hash_table_new (g_str_hash, g_str_equal);
551 idx = GPOINTER_TO_UINT (g_hash_table_lookup (table->hash, value));
554 idx = table->data->len;
555 g_string_append (table->data, value);
556 g_string_append_c (table->data, 0);
557 g_hash_table_insert (table->hash, (void*)value, GUINT_TO_POINTER (idx));
562 append_subsection (MonoImageWriter *acfg, ElfSectHeader *sheaders, BinSection *sect, BinSection *add)
564 int offset = sect->cur_offset;
565 /*offset += (sheaders [sect->shidx].sh_addralign - 1);
566 offset &= ~(sheaders [sect->shidx].sh_addralign - 1);*/
568 * FIXME: we shouldn't align subsections at all, but if we don't then the
569 * stuff inside the subsections which is aligned won't get aligned.
571 if (strcmp (sect->name, ".debug_line") != 0) {
575 bin_writer_emit_ensure_buffer (sect, offset);
576 //g_print ("section %s aligned to %d from %d\n", sect->name, offset, sect->cur_offset);
577 sect->cur_offset = offset;
579 bin_writer_emit_ensure_buffer (sect, add->cur_offset);
580 memcpy (sect->data + sect->cur_offset, add->data, add->cur_offset);
582 sect->cur_offset += add->cur_offset;
583 add->cur_offset = offset; /* it becomes the offset in the parent section */
584 //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);
589 /* merge the subsections */
591 collect_sections (MonoImageWriter *acfg, ElfSectHeader *sheaders, BinSection **out, int num)
593 int i, j, maxs, num_sections;
598 for (sect = acfg->sections; sect; sect = sect->next) {
599 if (sect->subsection == 0) {
600 out [num_sections++] = sect;
601 g_assert (num_sections < num);
603 maxs = MAX (maxs, sect->subsection);
605 for (i = 0; i < num_sections; i++) {
606 for (j = 1; j <= maxs; ++j) {
607 for (sect = acfg->sections; sect; sect = sect->next) {
608 if (sect->subsection == j && strcmp (out [i]->name, sect->name) == 0) {
609 append_subsection (acfg, sheaders, out [i], sect);
618 elf_hash (const unsigned char *name)
620 unsigned long h = 0, g;
622 h = (h << 4) + *name++;
623 if ((g = h & 0xf0000000))
630 #define NUM_BUCKETS 17
633 build_hash (MonoImageWriter *acfg, int num_sections, ElfStrTable *dynstr)
636 int num_symbols = 1 + num_sections + 3;
639 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
640 if (!symbol->is_global)
643 str_table_add (dynstr, symbol->name);
644 /*g_print ("adding sym: %s\n", symbol->name);*/
646 str_table_add (dynstr, "__bss_start");
647 str_table_add (dynstr, "_edata");
648 str_table_add (dynstr, "_end");
650 data = g_new0 (int, num_symbols + 2 + NUM_BUCKETS);
651 data [0] = NUM_BUCKETS;
652 data [1] = num_symbols;
658 get_label_addr (MonoImageWriter *acfg, const char *name)
665 lab = g_hash_table_lookup (acfg->labels, name);
667 g_error ("Undefined label: '%s'.\n", name);
668 section = lab->section;
669 offset = lab->offset;
670 if (section->parent) {
671 value = section->parent->virt_offset + section->cur_offset + offset;
673 value = section->virt_offset + offset;
679 collect_syms (MonoImageWriter *acfg, int *hash, ElfStrTable *strtab, ElfSectHeader *sheaders, int *num_syms)
690 symbols = g_new0 (ElfSymbol, hash [1]);
693 for (symbol = acfg->symbols; symbol; symbol = symbol->next)
696 symbols = g_new0 (ElfSymbol, i + SECT_NUM + 10); /* FIXME */
699 /* the first symbol is undef, all zeroes */
703 for (j = 1; j < SECT_NUM; ++j) {
704 symbols [i].st_info = ELF32_ST_INFO (STB_LOCAL, STT_SECTION);
705 symbols [i].st_shndx = j;
706 symbols [i].st_value = sheaders [j].sh_addr;
710 for (section = acfg->sections; section; section = section->next) {
713 symbols [i].st_info = ELF32_ST_INFO (STB_LOCAL, STT_SECTION);
714 if (strcmp (section->name, ".text") == 0) {
715 symbols [i].st_shndx = SECT_TEXT;
716 section->shidx = SECT_TEXT;
717 section->file_offset = 4096;
718 symbols [i].st_value = section->virt_offset;
719 } else if (strcmp (section->name, ".data") == 0) {
720 symbols [i].st_shndx = SECT_DATA;
721 section->shidx = SECT_DATA;
722 section->file_offset = 4096 + 28; /* FIXME */
723 symbols [i].st_value = section->virt_offset;
724 } else if (strcmp (section->name, ".bss") == 0) {
725 symbols [i].st_shndx = SECT_BSS;
726 section->shidx = SECT_BSS;
727 section->file_offset = 4096 + 28 + 8; /* FIXME */
728 symbols [i].st_value = section->virt_offset;
733 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
736 if (!symbol->is_global && hash)
738 symbols [i].st_info = ELF32_ST_INFO (symbol->is_global ? STB_GLOBAL : STB_LOCAL, symbol->is_function? STT_FUNC : STT_OBJECT);
739 symbols [i].st_name = str_table_add (strtab, symbol->name);
740 /*g_print ("sym name %s tabled to %d\n", symbol->name, symbols [i].st_name);*/
741 section = symbol->section;
742 symbols [i].st_shndx = section->parent? section->parent->shidx: section->shidx;
743 lab = g_hash_table_lookup (acfg->labels, symbol->name);
744 offset = lab->offset;
745 if (section->parent) {
746 symbols [i].st_value = section->parent->virt_offset + section->cur_offset + offset;
748 symbols [i].st_value = section->virt_offset + offset;
751 if (symbol->end_label) {
752 BinLabel *elab = g_hash_table_lookup (acfg->labels, symbol->end_label);
754 symbols [i].st_size = elab->offset - lab->offset;
758 /* add special symbols */
759 symbols [i].st_name = str_table_add (strtab, "__bss_start");
760 symbols [i].st_shndx = 0xfff1;
761 symbols [i].st_info = ELF32_ST_INFO (STB_GLOBAL, 0);
763 symbols [i].st_name = str_table_add (strtab, "_edata");
764 symbols [i].st_shndx = 0xfff1;
765 symbols [i].st_info = ELF32_ST_INFO (STB_GLOBAL, 0);
767 symbols [i].st_name = str_table_add (strtab, "_end");
768 symbols [i].st_shndx = 0xfff1;
769 symbols [i].st_info = ELF32_ST_INFO (STB_GLOBAL, 0);
775 /* add to hash table */
778 chain = hash + 2 + hash [0];
779 for (i = 0; i < hash [1]; ++i) {
781 /*g_print ("checking %d '%s' (sym %d)\n", symbols [i].st_name, strtab->data->str + symbols [i].st_name, i);*/
782 if (!symbols [i].st_name)
784 hashc = elf_hash ((guint8*)strtab->data->str + symbols [i].st_name);
785 slot = hashc % hash [0];
786 /*g_print ("hashing '%s' at slot %d (sym %d)\n", strtab->data->str + symbols [i].st_name, slot, i);*/
788 chain [i] = bucket [slot];
799 reloc_symbols (MonoImageWriter *acfg, ElfSymbol *symbols, ElfSectHeader *sheaders, ElfStrTable *strtab, gboolean dynamic)
807 for (section = acfg->sections; section; section = section->next) {
810 symbols [i].st_value = sheaders [section->shidx].sh_addr;
814 for (i = 1; i < SECT_NUM; ++i) {
815 symbols [i].st_value = sheaders [i].sh_addr;
818 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
821 if (dynamic && !symbol->is_global)
823 section = symbol->section;
824 lab = g_hash_table_lookup (acfg->labels, symbol->name);
825 offset = lab->offset;
826 if (section->parent) {
827 symbols [i].st_value = sheaders [section->parent->shidx].sh_addr + section->cur_offset + offset;
829 symbols [i].st_value = sheaders [section->shidx].sh_addr + offset;
834 symbols [i].st_value = sheaders [SECT_BSS].sh_addr;
837 symbols [i].st_value = sheaders [SECT_DATA].sh_addr + sheaders [SECT_DATA].sh_size;
840 symbols [i].st_value = sheaders [SECT_BSS].sh_addr + sheaders [SECT_BSS].sh_size;
845 resolve_reloc (MonoImageWriter *acfg, BinReloc *reloc, guint8 **out_data, gsize *out_vaddr, gsize *out_start_val, gsize *out_end_val)
848 gssize end_val, start_val;
851 end_val = get_label_addr (acfg, reloc->val1);
853 start_val = get_label_addr (acfg, reloc->val2);
854 } else if (reloc->val2_section) {
855 start_val = reloc->val2_offset;
856 if (reloc->val2_section->parent)
857 start_val += reloc->val2_section->parent->virt_offset + reloc->val2_section->cur_offset;
859 start_val += reloc->val2_section->virt_offset;
863 end_val = end_val - start_val + reloc->offset;
864 if (reloc->section->parent) {
865 data = reloc->section->parent->data;
866 data += reloc->section->cur_offset;
867 data += reloc->section_offset;
868 vaddr = reloc->section->parent->virt_offset;
869 vaddr += reloc->section->cur_offset;
870 vaddr += reloc->section_offset;
872 data = reloc->section->data;
873 data += reloc->section_offset;
874 vaddr = reloc->section->virt_offset;
875 vaddr += reloc->section_offset;
878 *out_start_val = start_val;
879 *out_end_val = end_val;
887 resolve_relocations (MonoImageWriter *acfg)
891 gsize end_val, start_val;
896 rr = g_new0 (ElfRelocA, acfg->num_relocs);
899 for (reloc = acfg->relocations; reloc; reloc = reloc->next) {
900 resolve_reloc (acfg, reloc, &data, &vaddr, &start_val, &end_val);
901 /* FIXME: little endian */
903 data [1] = end_val >> 8;
904 data [2] = end_val >> 16;
905 data [3] = end_val >> 24;
907 if (start_val == 0 && reloc->val1 [0] != '.') {
908 rr [i].r_offset = vaddr;
909 rr [i].r_info = R_X86_64_RELATIVE;
910 rr [i].r_addend = end_val;
912 g_assert (i <= acfg->num_relocs);
918 #else /* USE_ELF_RELA */
921 do_reloc (MonoImageWriter *acfg, BinReloc *reloc, guint8 *data, gssize addr)
925 * We use the official ARM relocation types, but implement only the stuff actually
926 * needed by the code we generate.
928 switch (reloc->reloc_type) {
931 guint32 *code = (guint32*)(gpointer)data;
935 if (reloc->reloc_type == R_ARM_CALL)
937 g_assert (data [3] == 0xeb);
940 g_assert (data [3] == 0xea);
941 if (diff >= 0 && diff <= 33554431) {
943 ins = (ins & 0xff000000) | diff;
945 } else if (diff <= 0 && diff >= -33554432) {
947 ins = (ins & 0xff000000) | (diff & ~0xff000000);
950 g_assert_not_reached ();
954 case R_ARM_ALU_PC_G0_NC: {
955 /* Generated by emit_plt () */
959 g_assert (val <= 0xffffff);
961 ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_PC, (val & 0xFF0000) >> 16, 16);
963 ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_PC, 0, 0);
964 ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_IP, (val & 0xFF00) >> 8, 24);
965 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, val & 0xFF);
969 g_assert_not_reached ();
972 g_assert_not_reached ();
977 resolve_relocations (MonoImageWriter *acfg)
981 gsize end_val, start_val;
986 rr = g_new0 (ElfReloc, acfg->num_relocs);
989 for (reloc = acfg->relocations; reloc; reloc = reloc->next) {
990 resolve_reloc (acfg, reloc, &data, &vaddr, &start_val, &end_val);
991 /* FIXME: little endian */
992 if (reloc->reloc_type) {
994 g_assert (start_val > 0);
995 do_reloc (acfg, reloc, data, end_val);
998 data [1] = end_val >> 8;
999 data [2] = end_val >> 16;
1000 data [3] = end_val >> 24;
1003 if (start_val == 0 && reloc->val1 [0] != '.') {
1004 rr [i].r_offset = vaddr;
1005 rr [i].r_info = R_386_RELATIVE;
1007 g_assert (i <= acfg->num_relocs);
1013 #endif /* USE_ELF_RELA */
1016 bin_writer_fwrite (MonoImageWriter *acfg, void *val, size_t size, size_t nmemb)
1019 fwrite (val, size, nmemb, acfg->fp);
1021 g_assert (acfg->out_buf_pos + (size * nmemb) <= acfg->out_buf_size);
1022 memcpy (acfg->out_buf + acfg->out_buf_pos, val, size * nmemb);
1023 acfg->out_buf_pos += (size * nmemb);
1028 bin_writer_fseek (MonoImageWriter *acfg, int offset)
1031 fseek (acfg->fp, offset, SEEK_SET);
1033 acfg->out_buf_pos = offset;
1036 static int normal_sections [] = { SECT_DATA, SECT_DEBUG_FRAME, SECT_DEBUG_INFO, SECT_DEBUG_ABBREV, SECT_DEBUG_LINE, SECT_DEBUG_LOC };
1039 bin_writer_emit_writeout (MonoImageWriter *acfg)
1043 ElfProgHeader progh [3];
1044 ElfSectHeader secth [SECT_NUM];
1050 ElfStrTable str_table = {NULL, NULL};
1051 ElfStrTable sh_str_table = {NULL, NULL};
1052 ElfStrTable dyn_str_table = {NULL, NULL};
1053 BinSection* all_sections [32];
1054 BinSection* sections [SECT_NUM];
1057 ElfDynamic dynamic [14];
1059 int i, num_sections, file_offset, virt_offset, size, num_symtab;
1064 /* Section headers */
1065 memset (§h, 0, sizeof (secth));
1066 memset (&dynamic, 0, sizeof (dynamic));
1067 memset (&header, 0, sizeof (header));
1069 for (i = 1; i < SECT_NUM; ++i) {
1070 secth [i].sh_name = str_table_add (&sh_str_table, section_info [i].name);
1071 secth [i].sh_type = section_info [i].type;
1072 secth [i].sh_addralign = section_info [i].align;
1073 secth [i].sh_flags = section_info [i].flags;
1074 secth [i].sh_entsize = section_info [i].esize;
1076 secth [SECT_DYNSYM].sh_info = SIZEOF_VOID_P == 4 ? 4 : 2;
1077 secth [SECT_SYMTAB].sh_info = SIZEOF_VOID_P == 4 ? 20 : 17;
1078 secth [SECT_HASH].sh_link = SECT_DYNSYM;
1079 secth [SECT_DYNSYM].sh_link = SECT_DYNSTR;
1080 secth [SECT_REL_DYN].sh_link = SECT_DYNSYM;
1081 secth [SECT_RELA_DYN].sh_link = SECT_DYNSYM;
1082 secth [SECT_DYNAMIC].sh_link = SECT_DYNSTR;
1083 secth [SECT_SYMTAB].sh_link = SECT_STRTAB;
1085 num_sections = collect_sections (acfg, secth, all_sections, 16);
1086 hash = build_hash (acfg, num_sections, &dyn_str_table);
1087 num_symtab = hash [1]; /* FIXME */
1089 g_print ("num_sections: %d\n", num_sections);
1090 g_print ("dynsym: %d, dynstr size: %d\n", hash [1], (int)dyn_str_table.data->len);
1091 for (i = 0; i < num_sections; ++i) {
1092 g_print ("section %s, size: %d, %x\n", all_sections [i]->name, all_sections [i]->cur_offset, all_sections [i]->cur_offset);
1095 /* Associate the bin sections with the ELF sections */
1096 memset (sections, 0, sizeof (sections));
1097 for (i = 0; i < num_sections; ++i) {
1098 BinSection *sect = all_sections [i];
1101 for (j = 0; j < SECT_NUM; ++j) {
1102 if (strcmp (sect->name, section_info [j].name) == 0) {
1108 sections [all_sections [i]->shidx] = sect;
1111 /* at this point we know where in the file the first segment sections go */
1112 dynsym = collect_syms (acfg, hash, &dyn_str_table, NULL, NULL);
1113 num_local_syms = hash [1];
1114 symtab = collect_syms (acfg, NULL, &str_table, secth, &num_local_syms);
1116 file_offset = virt_offset = sizeof (header) + sizeof (progh);
1117 secth [SECT_HASH].sh_addr = secth [SECT_HASH].sh_offset = file_offset;
1118 size = sizeof (int) * (2 + hash [0] + hash [1]);
1119 virt_offset = (file_offset += size);
1120 secth [SECT_HASH].sh_size = size;
1121 secth [SECT_DYNSYM].sh_addr = secth [SECT_DYNSYM].sh_offset = file_offset;
1122 size = sizeof (ElfSymbol) * hash [1];
1123 virt_offset = (file_offset += size);
1124 secth [SECT_DYNSYM].sh_size = size;
1125 secth [SECT_DYNSTR].sh_addr = secth [SECT_DYNSTR].sh_offset = file_offset;
1126 size = dyn_str_table.data->len;
1127 virt_offset = (file_offset += size);
1128 secth [SECT_DYNSTR].sh_size = size;
1130 file_offset &= ~(4-1);
1131 secth [SECT_REL_DYN].sh_addr = secth [SECT_REL_DYN].sh_offset = file_offset;
1132 #ifndef USE_ELF_RELA
1133 size = sizeof (ElfReloc) * acfg->num_relocs;
1137 virt_offset = (file_offset += size);
1138 secth [SECT_REL_DYN].sh_size = size;
1139 secth [SECT_RELA_DYN].sh_addr = secth [SECT_RELA_DYN].sh_offset = file_offset;
1141 size = sizeof (ElfRelocA) * acfg->num_relocs;
1145 virt_offset = (file_offset += size);
1146 secth [SECT_RELA_DYN].sh_size = size;
1148 file_offset = ALIGN_TO (file_offset, secth [SECT_TEXT].sh_addralign);
1149 virt_offset = file_offset;
1150 secth [SECT_TEXT].sh_addr = secth [SECT_TEXT].sh_offset = file_offset;
1151 if (sections [SECT_TEXT]) {
1152 if (sections [SECT_TEXT]->has_addr) {
1153 secth [SECT_TEXT].sh_addr = sections [SECT_TEXT]->addr;
1154 secth [SECT_TEXT].sh_flags &= ~SHF_ALLOC;
1156 size = sections [SECT_TEXT]->cur_offset;
1157 secth [SECT_TEXT].sh_size = size;
1158 file_offset += size;
1161 file_offset = ALIGN_TO (file_offset, secth [SECT_DYNAMIC].sh_addralign);
1162 virt_offset = file_offset;
1164 /* .dynamic, .got.plt, .data, .bss here */
1165 /* Have to increase the virt offset since these go to a separate segment */
1166 virt_offset += PAGESIZE;
1167 secth [SECT_DYNAMIC].sh_addr = virt_offset;
1168 secth [SECT_DYNAMIC].sh_offset = file_offset;
1169 size = sizeof (dynamic);
1170 secth [SECT_DYNAMIC].sh_size = size;
1171 file_offset += size;
1172 virt_offset += size;
1174 file_offset = ALIGN_TO (file_offset, secth [SECT_GOT_PLT].sh_addralign);
1175 virt_offset = ALIGN_TO (virt_offset, secth [SECT_GOT_PLT].sh_addralign);
1176 secth [SECT_GOT_PLT].sh_addr = virt_offset;
1177 secth [SECT_GOT_PLT].sh_offset = file_offset;
1178 size = 3 * SIZEOF_VOID_P;
1179 secth [SECT_GOT_PLT].sh_size = size;
1180 file_offset += size;
1181 virt_offset += size;
1183 file_offset = ALIGN_TO (file_offset, secth [SECT_DATA].sh_addralign);
1184 virt_offset = ALIGN_TO (virt_offset, secth [SECT_DATA].sh_addralign);
1185 secth [SECT_DATA].sh_addr = virt_offset;
1186 secth [SECT_DATA].sh_offset = file_offset;
1187 if (sections [SECT_DATA]) {
1188 size = sections [SECT_DATA]->cur_offset;
1189 secth [SECT_DATA].sh_size = size;
1190 file_offset += size;
1191 virt_offset += size;
1194 file_offset = ALIGN_TO (file_offset, secth [SECT_BSS].sh_addralign);
1195 virt_offset = ALIGN_TO (virt_offset, secth [SECT_BSS].sh_addralign);
1196 secth [SECT_BSS].sh_addr = virt_offset;
1197 secth [SECT_BSS].sh_offset = file_offset;
1198 if (sections [SECT_BSS]) {
1199 size = sections [SECT_BSS]->cur_offset;
1200 secth [SECT_BSS].sh_size = size;
1203 /* virtual doesn't matter anymore */
1204 file_offset = ALIGN_TO (file_offset, secth [SECT_DEBUG_FRAME].sh_addralign);
1205 secth [SECT_DEBUG_FRAME].sh_offset = file_offset;
1206 if (sections [SECT_DEBUG_FRAME])
1207 size = sections [SECT_DEBUG_FRAME]->cur_offset;
1210 secth [SECT_DEBUG_FRAME].sh_size = size;
1211 file_offset += size;
1213 secth [SECT_DEBUG_INFO].sh_offset = file_offset;
1214 if (sections [SECT_DEBUG_INFO])
1215 size = sections [SECT_DEBUG_INFO]->cur_offset;
1218 secth [SECT_DEBUG_INFO].sh_size = size;
1219 file_offset += size;
1221 secth [SECT_DEBUG_ABBREV].sh_offset = file_offset;
1222 if (sections [SECT_DEBUG_ABBREV])
1223 size = sections [SECT_DEBUG_ABBREV]->cur_offset;
1226 secth [SECT_DEBUG_ABBREV].sh_size = size;
1227 file_offset += size;
1229 secth [SECT_DEBUG_LINE].sh_offset = file_offset;
1230 if (sections [SECT_DEBUG_LINE])
1231 size = sections [SECT_DEBUG_LINE]->cur_offset;
1234 secth [SECT_DEBUG_LINE].sh_size = size;
1235 file_offset += size;
1237 secth [SECT_DEBUG_LOC].sh_offset = file_offset;
1238 if (sections [SECT_DEBUG_LOC])
1239 size = sections [SECT_DEBUG_LOC]->cur_offset;
1242 secth [SECT_DEBUG_LOC].sh_size = size;
1243 file_offset += size;
1245 file_offset = ALIGN_TO (file_offset, secth [SECT_SHSTRTAB].sh_addralign);
1246 secth [SECT_SHSTRTAB].sh_offset = file_offset;
1247 size = sh_str_table.data->len;
1248 secth [SECT_SHSTRTAB].sh_size = size;
1249 file_offset += size;
1251 file_offset = ALIGN_TO (file_offset, secth [SECT_SYMTAB].sh_addralign);
1252 secth [SECT_SYMTAB].sh_offset = file_offset;
1253 size = sizeof (ElfSymbol) * num_local_syms;
1254 secth [SECT_SYMTAB].sh_size = size;
1255 file_offset += size;
1257 file_offset = ALIGN_TO (file_offset, secth [SECT_STRTAB].sh_addralign);
1258 secth [SECT_STRTAB].sh_offset = file_offset;
1259 size = str_table.data->len;
1260 secth [SECT_STRTAB].sh_size = size;
1261 file_offset += size;
1263 for (i = 1; i < SECT_NUM; ++i) {
1264 if (section_info [i].esize != 0)
1265 g_assert (secth [i].sh_size % section_info [i].esize == 0);
1269 file_offset &= ~(4-1);
1271 header.e_ident [EI_MAG0] = ELFMAG0;
1272 header.e_ident [EI_MAG1] = ELFMAG1;
1273 header.e_ident [EI_MAG2] = ELFMAG2;
1274 header.e_ident [EI_MAG3] = ELFMAG3;
1275 header.e_ident [EI_CLASS] = SIZEOF_VOID_P == 4 ? ELFCLASS32 : ELFCLASS64;
1276 header.e_ident [EI_DATA] = ELFDATA2LSB;
1277 header.e_ident [EI_VERSION] = EV_CURRENT;
1278 header.e_ident [EI_OSABI] = ELFOSABI_NONE;
1279 header.e_ident [EI_ABIVERSION] = 0;
1280 for (i = EI_PAD; i < EI_NIDENT; ++i)
1281 header.e_ident [i] = 0;
1283 header.e_type = ET_DYN;
1284 #if defined(TARGET_X86)
1285 header.e_machine = EM_386;
1286 #elif defined(TARGET_AMD64)
1287 header.e_machine = EM_X86_64;
1288 #elif defined(TARGET_ARM)
1289 header.e_machine = EM_ARM;
1291 g_assert_not_reached ();
1293 header.e_version = 1;
1295 header.e_phoff = sizeof (header);
1296 header.e_ehsize = sizeof (header);
1297 header.e_phentsize = sizeof (ElfProgHeader);
1299 header.e_entry = secth [SECT_TEXT].sh_addr;
1300 header.e_shstrndx = SECT_SHSTRTAB;
1301 header.e_shentsize = sizeof (ElfSectHeader);
1302 header.e_shnum = SECT_NUM;
1303 header.e_shoff = file_offset;
1307 dynamic [i].d_tag = DT_HASH;
1308 dynamic [i].d_un.d_val = secth [SECT_HASH].sh_offset;
1310 dynamic [i].d_tag = DT_STRTAB;
1311 dynamic [i].d_un.d_val = secth [SECT_DYNSTR].sh_offset;
1313 dynamic [i].d_tag = DT_SYMTAB;
1314 dynamic [i].d_un.d_val = secth [SECT_DYNSYM].sh_offset;
1316 dynamic [i].d_tag = DT_STRSZ;
1317 dynamic [i].d_un.d_val = dyn_str_table.data->len;
1319 dynamic [i].d_tag = DT_SYMENT;
1320 dynamic [i].d_un.d_val = sizeof (ElfSymbol);
1323 dynamic [i].d_tag = DT_RELA;
1324 dynamic [i].d_un.d_val = secth [SECT_RELA_DYN].sh_offset;
1326 dynamic [i].d_tag = DT_RELASZ;
1327 dynamic [i].d_un.d_val = secth [SECT_RELA_DYN].sh_size;
1329 dynamic [i].d_tag = DT_RELAENT;
1330 dynamic [i].d_un.d_val = sizeof (ElfRelocA);
1333 dynamic [i].d_tag = DT_REL;
1334 dynamic [i].d_un.d_val = secth [SECT_REL_DYN].sh_offset;
1336 dynamic [i].d_tag = DT_RELSZ;
1337 dynamic [i].d_un.d_val = secth [SECT_REL_DYN].sh_size;
1339 dynamic [i].d_tag = DT_RELENT;
1340 dynamic [i].d_un.d_val = sizeof (ElfReloc);
1343 dynamic [i].d_tag = DT_RELCOUNT;
1344 dynamic [i].d_un.d_val = acfg->num_relocs;
1347 /* Program header */
1348 memset (&progh, 0, sizeof (progh));
1349 progh [0].p_type = PT_LOAD;
1350 progh [0].p_filesz = progh [0].p_memsz = secth [SECT_DYNAMIC].sh_offset;
1351 progh [0].p_align = 4096;
1352 progh [0].p_flags = 5;
1354 progh [1].p_type = PT_LOAD;
1355 progh [1].p_offset = secth [SECT_DYNAMIC].sh_offset;
1356 progh [1].p_vaddr = progh [1].p_paddr = secth [SECT_DYNAMIC].sh_addr;
1357 progh [1].p_filesz = secth [SECT_BSS].sh_offset - secth [SECT_DYNAMIC].sh_offset;
1358 progh [1].p_memsz = secth [SECT_BSS].sh_addr + secth [SECT_BSS].sh_size - secth [SECT_DYNAMIC].sh_addr;
1359 progh [1].p_align = 4096;
1360 progh [1].p_flags = 6;
1362 progh [2].p_type = PT_DYNAMIC;
1363 progh [2].p_offset = secth [SECT_DYNAMIC].sh_offset;
1364 progh [2].p_vaddr = progh [2].p_paddr = secth [SECT_DYNAMIC].sh_addr;
1365 progh [2].p_filesz = progh [2].p_memsz = secth [SECT_DYNAMIC].sh_size;
1366 progh [2].p_align = SIZEOF_VOID_P;
1367 progh [2].p_flags = 6;
1369 /* Compute the addresses of the bin sections, so relocation can be done */
1370 for (i = 0; i < SECT_NUM; ++i) {
1372 sections [i]->file_offset = secth [i].sh_offset;
1373 sections [i]->virt_offset = secth [i].sh_addr;
1377 reloc_symbols (acfg, dynsym, secth, &dyn_str_table, TRUE);
1378 reloc_symbols (acfg, symtab, secth, &str_table, FALSE);
1379 relocs = resolve_relocations (acfg);
1382 acfg->out_buf_size = file_offset + sizeof (secth);
1383 acfg->out_buf = g_malloc (acfg->out_buf_size);
1386 bin_writer_fwrite (acfg, &header, sizeof (header), 1);
1387 bin_writer_fwrite (acfg, &progh, sizeof (progh), 1);
1388 bin_writer_fwrite (acfg, hash, sizeof (int) * (hash [0] + hash [1] + 2), 1);
1389 bin_writer_fwrite (acfg, dynsym, sizeof (ElfSymbol) * hash [1], 1);
1390 bin_writer_fwrite (acfg, dyn_str_table.data->str, dyn_str_table.data->len, 1);
1392 bin_writer_fseek (acfg, secth [SECT_REL_DYN].sh_offset);
1393 bin_writer_fwrite (acfg, relocs, sizeof (ElfReloc), acfg->num_relocs);
1396 bin_writer_fseek (acfg, secth [SECT_RELA_DYN].sh_offset);
1397 bin_writer_fwrite (acfg, relocs, secth [SECT_RELA_DYN].sh_size, 1);
1400 if (sections [SECT_TEXT]) {
1401 bin_writer_fseek (acfg, secth [SECT_TEXT].sh_offset);
1402 bin_writer_fwrite (acfg, sections [SECT_TEXT]->data, sections [SECT_TEXT]->cur_offset, 1);
1405 bin_writer_fwrite (acfg, dynamic, sizeof (dynamic), 1);
1408 size = secth [SECT_DYNAMIC].sh_addr;
1409 bin_writer_fwrite (acfg, &size, sizeof (size), 1);
1411 /* normal sections */
1412 for (i = 0; i < sizeof (normal_sections) / sizeof (normal_sections [0]); ++i) {
1413 int sect = normal_sections [i];
1414 if (sections [sect]) {
1415 bin_writer_fseek (acfg, secth [sect].sh_offset);
1416 bin_writer_fwrite (acfg, sections [sect]->data, sections [sect]->cur_offset, 1);
1420 bin_writer_fseek (acfg, secth [SECT_SHSTRTAB].sh_offset);
1421 bin_writer_fwrite (acfg, sh_str_table.data->str, sh_str_table.data->len, 1);
1422 bin_writer_fseek (acfg, secth [SECT_SYMTAB].sh_offset);
1423 bin_writer_fwrite (acfg, symtab, sizeof (ElfSymbol) * num_local_syms, 1);
1424 bin_writer_fseek (acfg, secth [SECT_STRTAB].sh_offset);
1425 bin_writer_fwrite (acfg, str_table.data->str, str_table.data->len, 1);
1426 /*g_print ("file_offset %d vs %d\n", file_offset, ftell (file));*/
1427 /*g_assert (file_offset >= ftell (file));*/
1428 bin_writer_fseek (acfg, file_offset);
1429 bin_writer_fwrite (acfg, §h, sizeof (secth), 1);
1437 #endif /* USE_ELF_WRITER */
1439 #endif /* USE_BIN_WRITER */
1444 asm_writer_emit_start (MonoImageWriter *acfg)
1449 asm_writer_emit_writeout (MonoImageWriter *acfg)
1457 asm_writer_emit_unset_mode (MonoImageWriter *acfg)
1459 if (acfg->mode == EMIT_NONE)
1461 fprintf (acfg->fp, "\n");
1462 acfg->mode = EMIT_NONE;
1466 asm_writer_emit_section_change (MonoImageWriter *acfg, const char *section_name, int subsection_index)
1468 asm_writer_emit_unset_mode (acfg);
1469 #if defined(TARGET_ASM_APPLE)
1470 if (strcmp(section_name, ".bss") == 0)
1471 fprintf (acfg->fp, "%s\n", ".data");
1472 else if (strstr (section_name, ".debug") == section_name) {
1473 //g_assert (subsection_index == 0);
1474 fprintf (acfg->fp, ".section __DWARF, __%s,regular,debug\n", section_name + 1);
1476 fprintf (acfg->fp, "%s\n", section_name);
1477 #elif defined(TARGET_ARM) || defined(TARGET_POWERPC)
1478 /* ARM gas doesn't seem to like subsections of .bss */
1479 if (!strcmp (section_name, ".text") || !strcmp (section_name, ".data")) {
1480 fprintf (acfg->fp, "%s %d\n", section_name, subsection_index);
1482 fprintf (acfg->fp, ".section \"%s\"\n", section_name);
1483 fprintf (acfg->fp, ".subsection %d\n", subsection_index);
1485 #elif defined(HOST_WIN32)
1486 fprintf (acfg->fp, ".section %s\n", section_name);
1488 if (!strcmp (section_name, ".text") || !strcmp (section_name, ".data") || !strcmp (section_name, ".bss")) {
1489 fprintf (acfg->fp, "%s %d\n", section_name, subsection_index);
1491 fprintf (acfg->fp, ".section \"%s\"\n", section_name);
1492 fprintf (acfg->fp, ".subsection %d\n", subsection_index);
1498 const char *get_label (const char *s)
1500 #ifdef TARGET_ASM_APPLE
1501 if (s [0] == '.' && s [1] == 'L')
1502 /* apple uses "L" instead of ".L" to mark temporary labels */
1509 asm_writer_emit_symbol_type (MonoImageWriter *acfg, const char *name, gboolean func)
1518 asm_writer_emit_unset_mode (acfg);
1519 #if defined(TARGET_ASM_APPLE)
1521 #elif defined(TARGET_ARM)
1522 fprintf (acfg->fp, "\t.type %s,#%s\n", name, stype);
1524 fprintf (acfg->fp, "\t.type %s,@%s\n", name, stype);
1529 asm_writer_emit_global (MonoImageWriter *acfg, const char *name, gboolean func)
1531 asm_writer_emit_unset_mode (acfg);
1532 #if (defined(__ppc__) && defined(TARGET_ASM_APPLE)) || (defined(HOST_WIN32) && !defined(MONO_CROSS_COMPILE))
1533 // mach-o always uses a '_' prefix.
1534 fprintf (acfg->fp, "\t.globl _%s\n", name);
1536 fprintf (acfg->fp, "\t.globl %s\n", name);
1539 asm_writer_emit_symbol_type (acfg, name, func);
1543 asm_writer_emit_local_symbol (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean func)
1545 asm_writer_emit_unset_mode (acfg);
1547 #ifndef TARGET_ASM_APPLE
1548 fprintf (acfg->fp, "\t.local %s\n", name);
1551 asm_writer_emit_symbol_type (acfg, name, func);
1555 asm_writer_emit_symbol_size (MonoImageWriter *acfg, const char *name, const char *end_label)
1557 asm_writer_emit_unset_mode (acfg);
1559 #ifndef TARGET_ASM_APPLE
1560 fprintf (acfg->fp, "\t.size %s,%s-%s\n", name, end_label, name);
1565 asm_writer_emit_label (MonoImageWriter *acfg, const char *name)
1567 asm_writer_emit_unset_mode (acfg);
1568 #if defined(HOST_WIN32) && (defined(TARGET_X86) || defined(TARGET_AMD64))
1569 fprintf (acfg->fp, "_%s:\n", name);
1570 #if defined(HOST_WIN32)
1571 /* Emit a normal label too */
1572 fprintf (acfg->fp, "%s:\n", name);
1575 fprintf (acfg->fp, "%s:\n", get_label (name));
1581 asm_writer_emit_string (MonoImageWriter *acfg, const char *value)
1583 asm_writer_emit_unset_mode (acfg);
1584 fprintf (acfg->fp, "\t%s \"%s\"\n", AS_STRING_DIRECTIVE, value);
1588 asm_writer_emit_line (MonoImageWriter *acfg)
1590 asm_writer_emit_unset_mode (acfg);
1591 fprintf (acfg->fp, "\n");
1595 asm_writer_emit_alignment (MonoImageWriter *acfg, int size)
1597 asm_writer_emit_unset_mode (acfg);
1598 #if defined(TARGET_ARM)
1599 fprintf (acfg->fp, "\t.align %d\n", ilog2 (size));
1600 #elif defined(__ppc__) && defined(TARGET_ASM_APPLE)
1601 // the mach-o assembler specifies alignments as powers of 2.
1602 fprintf (acfg->fp, "\t.align %d\t; ilog2\n", ilog2(size));
1603 #elif defined(TARGET_ASM_GAS)
1604 fprintf (acfg->fp, "\t.balign %d\n", size);
1606 fprintf (acfg->fp, "\t.align %d\n", size);
1611 asm_writer_emit_pointer_unaligned (MonoImageWriter *acfg, const char *target)
1613 asm_writer_emit_unset_mode (acfg);
1614 fprintf (acfg->fp, "\t%s %s\n", AS_POINTER_DIRECTIVE, target ? target : "0");
1618 asm_writer_emit_pointer (MonoImageWriter *acfg, const char *target)
1620 asm_writer_emit_unset_mode (acfg);
1621 asm_writer_emit_alignment (acfg, sizeof (gpointer));
1622 asm_writer_emit_pointer_unaligned (acfg, target);
1625 static char *byte_to_str;
1628 asm_writer_emit_bytes (MonoImageWriter *acfg, const guint8* buf, int size)
1631 if (acfg->mode != EMIT_BYTE) {
1632 acfg->mode = EMIT_BYTE;
1633 acfg->col_count = 0;
1636 if (byte_to_str == NULL) {
1637 byte_to_str = g_new0 (char, 256 * 8);
1638 for (i = 0; i < 256; ++i) {
1639 sprintf (byte_to_str + (i * 8), ",%d", i);
1643 for (i = 0; i < size; ++i, ++acfg->col_count) {
1644 if ((acfg->col_count % 32) == 0)
1645 fprintf (acfg->fp, "\n\t.byte %d", buf [i]);
1647 fputs (byte_to_str + (buf [i] * 8), acfg->fp);
1652 asm_writer_emit_int16 (MonoImageWriter *acfg, int value)
1654 if (acfg->mode != EMIT_WORD) {
1655 acfg->mode = EMIT_WORD;
1656 acfg->col_count = 0;
1658 if ((acfg->col_count++ % 8) == 0)
1659 fprintf (acfg->fp, "\n\t%s ", AS_INT16_DIRECTIVE);
1661 fprintf (acfg->fp, ", ");
1662 fprintf (acfg->fp, "%d", value);
1666 asm_writer_emit_int32 (MonoImageWriter *acfg, int value)
1668 if (acfg->mode != EMIT_LONG) {
1669 acfg->mode = EMIT_LONG;
1670 acfg->col_count = 0;
1672 if ((acfg->col_count++ % 8) == 0)
1673 fprintf (acfg->fp, "\n\t%s ", AS_INT32_DIRECTIVE);
1675 fprintf (acfg->fp, ",");
1676 fprintf (acfg->fp, "%d", value);
1680 asm_writer_emit_symbol_diff (MonoImageWriter *acfg, const char *end, const char* start, int offset)
1682 #ifdef TARGET_ASM_APPLE
1686 if (acfg->mode != EMIT_LONG) {
1687 acfg->mode = EMIT_LONG;
1688 acfg->col_count = 0;
1691 // FIXME: This doesn't seem to work on the iphone
1693 //#ifdef TARGET_ASM_APPLE
1694 /* The apple assembler needs a separate symbol to be able to handle complex expressions */
1695 sprintf (symbol, "LTMP_SYM%d", acfg->label_gen);
1696 start = get_label (start);
1697 end = get_label (end);
1700 fprintf (acfg->fp, "\n%s=%s - %s + %d", symbol, end, start, offset);
1701 else if (offset < 0)
1702 fprintf (acfg->fp, "\n%s=%s - %s %d", symbol, end, start, offset);
1704 fprintf (acfg->fp, "\n%s=%s - %s", symbol, end, start);
1706 fprintf (acfg->fp, "\n\t%s ", AS_INT32_DIRECTIVE);
1707 fprintf (acfg->fp, "%s", symbol);
1709 start = get_label (start);
1710 end = get_label (end);
1711 if ((acfg->col_count++ % 8) == 0)
1712 fprintf (acfg->fp, "\n\t%s ", AS_INT32_DIRECTIVE);
1714 fprintf (acfg->fp, ",");
1716 fprintf (acfg->fp, "%s - %s + %d", end, start, offset);
1717 else if (offset < 0)
1718 fprintf (acfg->fp, "%s - %s %d", end, start, offset);
1720 fprintf (acfg->fp, "%s - %s", end, start);
1725 asm_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
1727 asm_writer_emit_unset_mode (acfg);
1728 fprintf (acfg->fp, "\t%s %d\n", AS_SKIP_DIRECTIVE, num);
1731 /* EMIT FUNCTIONS */
1734 img_writer_emit_start (MonoImageWriter *acfg)
1736 #ifdef USE_BIN_WRITER
1737 if (acfg->use_bin_writer)
1738 bin_writer_emit_start (acfg);
1740 asm_writer_emit_start (acfg);
1742 asm_writer_emit_start (acfg);
1747 img_writer_emit_section_change (MonoImageWriter *acfg, const char *section_name, int subsection_index)
1749 #ifdef USE_BIN_WRITER
1750 if (acfg->use_bin_writer)
1751 bin_writer_emit_section_change (acfg, section_name, subsection_index);
1753 asm_writer_emit_section_change (acfg, section_name, subsection_index);
1755 asm_writer_emit_section_change (acfg, section_name, subsection_index);
1758 acfg->current_section = section_name;
1759 acfg->current_subsection = subsection_index;
1763 img_writer_emit_push_section (MonoImageWriter *acfg, const char *section_name, int subsection)
1765 g_assert (acfg->stack_pos < 16 - 1);
1766 acfg->section_stack [acfg->stack_pos] = acfg->current_section;
1767 acfg->subsection_stack [acfg->stack_pos] = acfg->current_subsection;
1770 img_writer_emit_section_change (acfg, section_name, subsection);
1774 img_writer_emit_pop_section (MonoImageWriter *acfg)
1776 g_assert (acfg->stack_pos > 0);
1778 img_writer_emit_section_change (acfg, acfg->section_stack [acfg->stack_pos], acfg->subsection_stack [acfg->stack_pos]);
1782 img_writer_set_section_addr (MonoImageWriter *acfg, guint64 addr)
1784 #ifdef USE_BIN_WRITER
1785 if (!acfg->use_bin_writer)
1788 bin_writer_set_section_addr (acfg, addr);
1795 img_writer_emit_global (MonoImageWriter *acfg, const char *name, gboolean func)
1797 #ifdef USE_BIN_WRITER
1798 if (acfg->use_bin_writer)
1799 bin_writer_emit_global (acfg, name, func);
1801 asm_writer_emit_global (acfg, name, func);
1803 asm_writer_emit_global (acfg, name, func);
1808 img_writer_emit_local_symbol (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean func)
1810 #ifdef USE_BIN_WRITER
1811 if (acfg->use_bin_writer)
1812 bin_writer_emit_local_symbol (acfg, name, end_label, func);
1814 asm_writer_emit_local_symbol (acfg, name, end_label, func);
1816 asm_writer_emit_local_symbol (acfg, name, end_label, func);
1821 img_writer_emit_symbol_size (MonoImageWriter *acfg, const char *name, const char *end_label)
1823 if (!acfg->use_bin_writer)
1824 asm_writer_emit_symbol_size (acfg, name, end_label);
1828 img_writer_emit_label (MonoImageWriter *acfg, const char *name)
1830 #ifdef USE_BIN_WRITER
1831 if (acfg->use_bin_writer)
1832 bin_writer_emit_label (acfg, name);
1834 asm_writer_emit_label (acfg, name);
1836 asm_writer_emit_label (acfg, name);
1841 img_writer_emit_bytes (MonoImageWriter *acfg, const guint8* buf, int size)
1843 #ifdef USE_BIN_WRITER
1844 if (acfg->use_bin_writer)
1845 bin_writer_emit_bytes (acfg, buf, size);
1847 asm_writer_emit_bytes (acfg, buf, size);
1849 asm_writer_emit_bytes (acfg, buf, size);
1854 img_writer_emit_string (MonoImageWriter *acfg, const char *value)
1856 #ifdef USE_BIN_WRITER
1857 if (acfg->use_bin_writer)
1858 bin_writer_emit_string (acfg, value);
1860 asm_writer_emit_string (acfg, value);
1862 asm_writer_emit_string (acfg, value);
1867 img_writer_emit_line (MonoImageWriter *acfg)
1869 #ifdef USE_BIN_WRITER
1870 if (acfg->use_bin_writer)
1871 bin_writer_emit_line (acfg);
1873 asm_writer_emit_line (acfg);
1875 asm_writer_emit_line (acfg);
1880 img_writer_emit_alignment (MonoImageWriter *acfg, int size)
1882 #ifdef USE_BIN_WRITER
1883 if (acfg->use_bin_writer)
1884 bin_writer_emit_alignment (acfg, size);
1886 asm_writer_emit_alignment (acfg, size);
1888 asm_writer_emit_alignment (acfg, size);
1893 img_writer_emit_pointer_unaligned (MonoImageWriter *acfg, const char *target)
1895 #ifdef USE_BIN_WRITER
1896 if (acfg->use_bin_writer)
1897 bin_writer_emit_pointer_unaligned (acfg, target);
1899 asm_writer_emit_pointer_unaligned (acfg, target);
1901 asm_writer_emit_pointer_unaligned (acfg, target);
1906 img_writer_emit_pointer (MonoImageWriter *acfg, const char *target)
1908 #ifdef USE_BIN_WRITER
1909 if (acfg->use_bin_writer)
1910 bin_writer_emit_pointer (acfg, target);
1912 asm_writer_emit_pointer (acfg, target);
1914 asm_writer_emit_pointer (acfg, target);
1919 img_writer_emit_int16 (MonoImageWriter *acfg, int value)
1921 #ifdef USE_BIN_WRITER
1922 if (acfg->use_bin_writer)
1923 bin_writer_emit_int16 (acfg, value);
1925 asm_writer_emit_int16 (acfg, value);
1927 asm_writer_emit_int16 (acfg, value);
1932 img_writer_emit_int32 (MonoImageWriter *acfg, int value)
1934 #ifdef USE_BIN_WRITER
1935 if (acfg->use_bin_writer)
1936 bin_writer_emit_int32 (acfg, value);
1938 asm_writer_emit_int32 (acfg, value);
1940 asm_writer_emit_int32 (acfg, value);
1945 img_writer_emit_symbol_diff (MonoImageWriter *acfg, const char *end, const char* start, int offset)
1947 #ifdef USE_BIN_WRITER
1948 if (acfg->use_bin_writer)
1949 bin_writer_emit_symbol_diff (acfg, end, start, offset);
1951 asm_writer_emit_symbol_diff (acfg, end, start, offset);
1953 asm_writer_emit_symbol_diff (acfg, end, start, offset);
1958 img_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
1960 #ifdef USE_BIN_WRITER
1961 if (acfg->use_bin_writer)
1962 bin_writer_emit_zero_bytes (acfg, num);
1964 asm_writer_emit_zero_bytes (acfg, num);
1966 asm_writer_emit_zero_bytes (acfg, num);
1971 img_writer_emit_writeout (MonoImageWriter *acfg)
1973 #ifdef USE_BIN_WRITER
1974 if (acfg->use_bin_writer)
1975 return bin_writer_emit_writeout (acfg);
1977 return asm_writer_emit_writeout (acfg);
1979 return asm_writer_emit_writeout (acfg);
1984 img_writer_emit_byte (MonoImageWriter *acfg, guint8 val)
1986 img_writer_emit_bytes (acfg, &val, 1);
1990 * Emit a relocation entry of type RELOC_TYPE against symbol SYMBOL at the current PC.
1991 * Do not advance PC.
1994 img_writer_emit_reloc (MonoImageWriter *acfg, int reloc_type, const char *symbol, int addend)
1996 /* This is only supported by the bin writer */
1997 #ifdef USE_BIN_WRITER
1998 if (acfg->use_bin_writer)
1999 bin_writer_emit_reloc (acfg, reloc_type, symbol, addend);
2001 g_assert_not_reached ();
2003 g_assert_not_reached ();
2008 * img_writer_emit_unset_mode:
2010 * Flush buffered data so it is safe to write to the output file from outside this
2011 * module. This is a nop for the binary writer.
2014 img_writer_emit_unset_mode (MonoImageWriter *acfg)
2016 if (!acfg->use_bin_writer)
2017 asm_writer_emit_unset_mode (acfg);
2021 * img_writer_get_output:
2023 * Return the output buffer of a binary writer emitting to memory. The returned memory
2024 * is from malloc, and it is owned by the caller.
2027 img_writer_get_output (MonoImageWriter *acfg, guint32 *size)
2029 #ifdef USE_BIN_WRITER
2032 g_assert (acfg->use_bin_writer);
2034 buf = acfg->out_buf;
2035 *size = acfg->out_buf_size;
2036 acfg->out_buf = NULL;
2039 g_assert_not_reached ();
2045 * Return whenever the binary writer is supported on this platform.
2048 bin_writer_supported (void)
2050 #ifdef USE_BIN_WRITER
2058 * img_writer_create:
2060 * Create an image writer writing to FP. If USE_BIN_WRITER is TRUE, FP can be NULL,
2061 * in this case the image writer will write to a memory buffer obtainable by calling
2062 * img_writer_get_output ().
2065 img_writer_create (FILE *fp, gboolean use_bin_writer)
2067 MonoImageWriter *w = g_new0 (MonoImageWriter, 1);
2069 #ifndef USE_BIN_WRITER
2070 g_assert (!use_bin_writer);
2073 if (!use_bin_writer)
2077 w->use_bin_writer = use_bin_writer;
2078 w->mempool = mono_mempool_new ();
2084 img_writer_destroy (MonoImageWriter *w)
2086 // FIXME: Free all the stuff
2087 mono_mempool_destroy (w->mempool);
2092 img_writer_subsections_supported (MonoImageWriter *acfg)
2094 #ifdef TARGET_ASM_APPLE
2095 return acfg->use_bin_writer;
2102 img_writer_get_fp (MonoImageWriter *acfg)
2108 img_writer_get_temp_label_prefix (MonoImageWriter *acfg)
2110 return AS_TEMP_LABEL_PREFIX;