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>
23 #ifndef PLATFORM_WIN32
32 #include <limits.h> /* for PAGESIZE */
37 #include "image-writer.h"
39 #ifndef PLATFORM_WIN32
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(__ppc__) || defined(__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)
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"
96 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
97 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
98 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
100 #if defined(TARGET_AMD64) && !defined(PLATFORM_WIN32)
101 #define USE_ELF_WRITER 1
102 #define USE_ELF_RELA 1
105 #if defined(TARGET_X86) && !defined(PLATFORM_WIN32)
106 #define USE_ELF_WRITER 1
109 #if defined(TARGET_ARM) && !defined(__MACH__)
110 #define USE_ELF_WRITER 1
113 #if defined(__mips__)
114 #define USE_ELF_WRITER 1
117 #if defined(USE_ELF_WRITER)
118 #define USE_BIN_WRITER 1
121 #ifdef USE_BIN_WRITER
123 typedef struct _BinSymbol BinSymbol;
124 typedef struct _BinReloc BinReloc;
125 typedef struct _BinSection BinSection;
137 struct _MonoImageWriter {
138 MonoMemPool *mempool;
140 gboolean use_bin_writer;
141 const char *current_section;
142 int current_subsection;
143 const char *section_stack [16];
144 int subsection_stack [16];
148 #ifdef USE_BIN_WRITER
150 BinSection *sections;
151 BinSection *cur_section;
152 BinReloc *relocations;
158 int mode; /* emit mode */
159 int col_count; /* bytes emitted per .byte line */
162 static G_GNUC_UNUSED int
163 ilog2(register int value)
166 while (value & ~0xf) count += 4, value >>= 4;
167 while (value) count++, value >>= 1;
171 #ifdef USE_BIN_WRITER
173 typedef struct _BinLabel BinLabel;
184 BinSection *val2_section;
197 gboolean is_function;
216 bin_writer_emit_start (MonoImageWriter *acfg)
218 acfg->labels = g_hash_table_new (g_str_hash, g_str_equal);
222 bin_writer_emit_section_change (MonoImageWriter *acfg, const char *section_name, int subsection_index)
226 if (acfg->cur_section && acfg->cur_section->subsection == subsection_index
227 && strcmp (acfg->cur_section->name, section_name) == 0)
229 for (section = acfg->sections; section; section = section->next) {
230 if (section->subsection == subsection_index && strcmp (section->name, section_name) == 0) {
231 acfg->cur_section = section;
236 section = g_new0 (BinSection, 1);
237 section->name = g_strdup (section_name);
238 section->subsection = subsection_index;
239 section->next = acfg->sections;
240 acfg->sections = section;
241 acfg->cur_section = section;
246 bin_writer_emit_symbol_inner (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean is_global, gboolean func)
248 BinSymbol *symbol = g_new0 (BinSymbol, 1);
249 symbol->name = g_strdup (name);
251 symbol->end_label = g_strdup (end_label);
252 symbol->is_function = func;
253 symbol->is_global = is_global;
254 symbol->section = acfg->cur_section;
255 /* FIXME: we align after this call... */
256 symbol->offset = symbol->section->cur_offset;
257 symbol->next = acfg->symbols;
258 acfg->symbols = symbol;
262 bin_writer_emit_global (MonoImageWriter *acfg, const char *name, gboolean func)
264 bin_writer_emit_symbol_inner (acfg, name, NULL, TRUE, func);
268 bin_writer_emit_local_symbol (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean func)
270 bin_writer_emit_symbol_inner (acfg, name, end_label, FALSE, func);
274 bin_writer_emit_label (MonoImageWriter *acfg, const char *name)
276 BinLabel *label = g_new0 (BinLabel, 1);
277 label->name = g_strdup (name);
278 label->section = acfg->cur_section;
279 label->offset = acfg->cur_section->cur_offset;
280 g_hash_table_insert (acfg->labels, label->name, label);
284 bin_writer_emit_ensure_buffer (BinSection *section, int size)
286 int new_offset = section->cur_offset + size;
287 if (new_offset >= section->data_len) {
288 int new_size = section->data_len? section->data_len * 2: 256;
290 while (new_size <= new_offset)
292 data = g_malloc0 (new_size);
293 memcpy (data, section->data, section->data_len);
294 g_free (section->data);
295 section->data = data;
296 section->data_len = new_size;
301 bin_writer_emit_bytes (MonoImageWriter *acfg, const guint8* buf, int size)
303 bin_writer_emit_ensure_buffer (acfg->cur_section, size);
304 memcpy (acfg->cur_section->data + acfg->cur_section->cur_offset, buf, size);
305 acfg->cur_section->cur_offset += size;
309 bin_writer_emit_string (MonoImageWriter *acfg, const char *value)
311 int size = strlen (value) + 1;
312 bin_writer_emit_bytes (acfg, (const guint8*)value, size);
316 bin_writer_emit_line (MonoImageWriter *acfg)
318 /* Nothing to do in binary writer */
322 bin_writer_emit_alignment (MonoImageWriter *acfg, int size)
324 int offset = acfg->cur_section->cur_offset;
326 offset += (size - 1);
327 offset &= ~(size - 1);
328 add = offset - acfg->cur_section->cur_offset;
330 bin_writer_emit_ensure_buffer (acfg->cur_section, add);
331 acfg->cur_section->cur_offset += add;
336 bin_writer_emit_pointer_unaligned (MonoImageWriter *acfg, const char *target)
342 g_assert_not_reached ();
343 reloc = g_new0 (BinReloc, 1);
344 reloc->val1 = g_strdup (target);
345 reloc->section = acfg->cur_section;
346 reloc->section_offset = acfg->cur_section->cur_offset;
347 reloc->next = acfg->relocations;
348 acfg->relocations = reloc;
349 if (strcmp (reloc->section->name, ".data") == 0) {
351 //g_print ("reloc: %s at %d\n", target, acfg->cur_section->cur_offset);
353 acfg->cur_section->cur_offset += sizeof (gpointer);
357 bin_writer_emit_pointer (MonoImageWriter *acfg, const char *target)
359 bin_writer_emit_alignment (acfg, sizeof (gpointer));
360 bin_writer_emit_pointer_unaligned (acfg, target);
364 bin_writer_emit_int16 (MonoImageWriter *acfg, int value)
367 bin_writer_emit_ensure_buffer (acfg->cur_section, 2);
368 data = acfg->cur_section->data + acfg->cur_section->cur_offset;
369 acfg->cur_section->cur_offset += 2;
370 /* FIXME: little endian */
372 data [1] = value >> 8;
376 bin_writer_emit_int32 (MonoImageWriter *acfg, int value)
379 bin_writer_emit_ensure_buffer (acfg->cur_section, 4);
380 data = acfg->cur_section->data + acfg->cur_section->cur_offset;
381 acfg->cur_section->cur_offset += 4;
382 /* FIXME: little endian */
384 data [1] = value >> 8;
385 data [2] = value >> 16;
386 data [3] = value >> 24;
390 create_reloc (MonoImageWriter *acfg, const char *end, const char* start, int offset)
393 reloc = mono_mempool_alloc0 (acfg->mempool, sizeof (BinReloc));
394 reloc->val1 = mono_mempool_strdup (acfg->mempool, end);
395 if (strcmp (start, ".") == 0) {
396 reloc->val2_section = acfg->cur_section;
397 reloc->val2_offset = acfg->cur_section->cur_offset;
399 reloc->val2 = mono_mempool_strdup (acfg->mempool, start);
401 reloc->offset = offset;
402 reloc->section = acfg->cur_section;
403 reloc->section_offset = acfg->cur_section->cur_offset;
404 reloc->next = acfg->relocations;
405 acfg->relocations = reloc;
410 bin_writer_emit_symbol_diff (MonoImageWriter *acfg, const char *end, const char* start, int offset)
412 create_reloc (acfg, end, start, offset);
413 acfg->cur_section->cur_offset += 4;
414 /*if (strcmp (reloc->section->name, ".data") == 0) {
416 g_print ("reloc: %s - %s + %d at %d\n", end, start, offset, acfg->cur_section->cur_offset - 4);
421 * Emit a relocation entry of type RELOC_TYPE against symbol SYMBOL at the current PC.
424 static G_GNUC_UNUSED void
425 bin_writer_emit_reloc (MonoImageWriter *acfg, int reloc_type, const char *symbol, int addend)
427 BinReloc *reloc = create_reloc (acfg, symbol, ".", addend);
428 reloc->reloc_type = reloc_type;
432 bin_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
434 bin_writer_emit_ensure_buffer (acfg->cur_section, num);
435 acfg->cur_section->cur_offset += num;
438 #ifdef USE_ELF_WRITER
463 #if SIZEOF_VOID_P == 4
465 typedef Elf32_Ehdr ElfHeader;
466 typedef Elf32_Shdr ElfSectHeader;
467 typedef Elf32_Phdr ElfProgHeader;
468 typedef Elf32_Sym ElfSymbol;
469 typedef Elf32_Rel ElfReloc;
470 typedef Elf32_Rela ElfRelocA;
471 typedef Elf32_Dyn ElfDynamic;
475 typedef Elf64_Ehdr ElfHeader;
476 typedef Elf64_Shdr ElfSectHeader;
477 typedef Elf64_Phdr ElfProgHeader;
478 typedef Elf64_Sym ElfSymbol;
479 typedef Elf64_Rel ElfReloc;
480 typedef Elf64_Rela ElfRelocA;
481 typedef Elf64_Dyn ElfDynamic;
493 static SectInfo section_info [] = {
495 {".hash", SHT_HASH, 4, 2, SIZEOF_VOID_P},
496 {".dynsym", SHT_DYNSYM, sizeof (ElfSymbol), 2, SIZEOF_VOID_P},
497 {".dynstr", SHT_STRTAB, 0, 2, 1},
498 {".rel.dyn", SHT_REL, sizeof (ElfReloc), 2, SIZEOF_VOID_P},
499 {".rela.dyn", SHT_RELA, sizeof (ElfRelocA), 2, SIZEOF_VOID_P},
500 {".text", SHT_PROGBITS, 0, 6, 4096},
501 {".dynamic", SHT_DYNAMIC, sizeof (ElfDynamic), 3, SIZEOF_VOID_P},
502 {".got.plt", SHT_PROGBITS, SIZEOF_VOID_P, 3, SIZEOF_VOID_P},
503 {".data", SHT_PROGBITS, 0, 3, 8},
504 {".bss", SHT_NOBITS, 0, 3, 8},
505 {".debug_frame", SHT_PROGBITS, 0, 0, 8},
506 {".debug_info", SHT_PROGBITS, 0, 0, 1},
507 {".debug_abbrev", SHT_PROGBITS, 0, 0, 1},
508 {".debug_line", SHT_PROGBITS, 0, 0, 1},
509 {".debug_loc", SHT_PROGBITS, 0, 0, 1},
510 {".shstrtab", SHT_STRTAB, 0, 0, 1},
511 {".symtab", SHT_SYMTAB, sizeof (ElfSymbol), 0, SIZEOF_VOID_P},
512 {".strtab", SHT_STRTAB, 0, 0, 1}
521 str_table_add (ElfStrTable *table, const char* value)
525 table->data = g_string_new_len ("", 1);
526 table->hash = g_hash_table_new (g_str_hash, g_str_equal);
528 idx = GPOINTER_TO_UINT (g_hash_table_lookup (table->hash, value));
531 idx = table->data->len;
532 g_string_append (table->data, value);
533 g_string_append_c (table->data, 0);
534 g_hash_table_insert (table->hash, (void*)value, GUINT_TO_POINTER (idx));
539 append_subsection (MonoImageWriter *acfg, ElfSectHeader *sheaders, BinSection *sect, BinSection *add)
541 int offset = sect->cur_offset;
542 /*offset += (sheaders [sect->shidx].sh_addralign - 1);
543 offset &= ~(sheaders [sect->shidx].sh_addralign - 1);*/
545 * FIXME: we shouldn't align subsections at all, but if we don't then the
546 * stuff inside the subsections which is aligned won't get aligned.
548 if (strcmp (sect->name, ".debug_line") != 0) {
552 bin_writer_emit_ensure_buffer (sect, offset);
553 //g_print ("section %s aligned to %d from %d\n", sect->name, offset, sect->cur_offset);
554 sect->cur_offset = offset;
556 bin_writer_emit_ensure_buffer (sect, add->cur_offset);
557 memcpy (sect->data + sect->cur_offset, add->data, add->cur_offset);
559 sect->cur_offset += add->cur_offset;
560 add->cur_offset = offset; /* it becomes the offset in the parent section */
561 //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);
566 /* merge the subsections */
568 collect_sections (MonoImageWriter *acfg, ElfSectHeader *sheaders, BinSection **out, int num)
570 int i, j, maxs, num_sections;
575 for (sect = acfg->sections; sect; sect = sect->next) {
576 if (sect->subsection == 0) {
577 out [num_sections++] = sect;
578 g_assert (num_sections < num);
580 maxs = MAX (maxs, sect->subsection);
582 for (i = 0; i < num_sections; i++) {
583 for (j = 1; j <= maxs; ++j) {
584 for (sect = acfg->sections; sect; sect = sect->next) {
585 if (sect->subsection == j && strcmp (out [i]->name, sect->name) == 0) {
586 append_subsection (acfg, sheaders, out [i], sect);
595 elf_hash (const unsigned char *name)
597 unsigned long h = 0, g;
599 h = (h << 4) + *name++;
600 if ((g = h & 0xf0000000))
607 #define NUM_BUCKETS 17
610 build_hash (MonoImageWriter *acfg, int num_sections, ElfStrTable *dynstr)
613 int num_symbols = 1 + num_sections + 3;
616 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
617 if (!symbol->is_global)
620 str_table_add (dynstr, symbol->name);
621 /*g_print ("adding sym: %s\n", symbol->name);*/
623 str_table_add (dynstr, "__bss_start");
624 str_table_add (dynstr, "_edata");
625 str_table_add (dynstr, "_end");
627 data = g_new0 (int, num_symbols + 2 + NUM_BUCKETS);
628 data [0] = NUM_BUCKETS;
629 data [1] = num_symbols;
635 get_label_addr (MonoImageWriter *acfg, const char *name)
642 lab = g_hash_table_lookup (acfg->labels, name);
644 g_error ("Undefined label: '%s'.\n", name);
645 section = lab->section;
646 offset = lab->offset;
647 if (section->parent) {
648 value = section->parent->virt_offset + section->cur_offset + offset;
650 value = section->virt_offset + offset;
656 collect_syms (MonoImageWriter *acfg, int *hash, ElfStrTable *strtab, ElfSectHeader *sheaders, int *num_syms)
667 symbols = g_new0 (ElfSymbol, hash [1]);
670 for (symbol = acfg->symbols; symbol; symbol = symbol->next)
673 symbols = g_new0 (ElfSymbol, i + SECT_NUM + 10); /* FIXME */
676 /* the first symbol is undef, all zeroes */
680 for (j = 1; j < SECT_NUM; ++j) {
681 symbols [i].st_info = ELF32_ST_INFO (STB_LOCAL, STT_SECTION);
682 symbols [i].st_shndx = j;
683 symbols [i].st_value = sheaders [j].sh_addr;
687 for (section = acfg->sections; section; section = section->next) {
690 symbols [i].st_info = ELF32_ST_INFO (STB_LOCAL, STT_SECTION);
691 if (strcmp (section->name, ".text") == 0) {
692 symbols [i].st_shndx = SECT_TEXT;
693 section->shidx = SECT_TEXT;
694 section->file_offset = 4096;
695 symbols [i].st_value = section->virt_offset;
696 } else if (strcmp (section->name, ".data") == 0) {
697 symbols [i].st_shndx = SECT_DATA;
698 section->shidx = SECT_DATA;
699 section->file_offset = 4096 + 28; /* FIXME */
700 symbols [i].st_value = section->virt_offset;
701 } else if (strcmp (section->name, ".bss") == 0) {
702 symbols [i].st_shndx = SECT_BSS;
703 section->shidx = SECT_BSS;
704 section->file_offset = 4096 + 28 + 8; /* FIXME */
705 symbols [i].st_value = section->virt_offset;
710 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
713 if (!symbol->is_global && hash)
715 symbols [i].st_info = ELF32_ST_INFO (symbol->is_global ? STB_GLOBAL : STB_LOCAL, symbol->is_function? STT_FUNC : STT_OBJECT);
716 symbols [i].st_name = str_table_add (strtab, symbol->name);
717 /*g_print ("sym name %s tabled to %d\n", symbol->name, symbols [i].st_name);*/
718 section = symbol->section;
719 symbols [i].st_shndx = section->parent? section->parent->shidx: section->shidx;
720 lab = g_hash_table_lookup (acfg->labels, symbol->name);
721 offset = lab->offset;
722 if (section->parent) {
723 symbols [i].st_value = section->parent->virt_offset + section->cur_offset + offset;
725 symbols [i].st_value = section->virt_offset + offset;
728 if (symbol->end_label) {
729 BinLabel *elab = g_hash_table_lookup (acfg->labels, symbol->end_label);
731 symbols [i].st_size = elab->offset - lab->offset;
735 /* add special symbols */
736 symbols [i].st_name = str_table_add (strtab, "__bss_start");
737 symbols [i].st_shndx = 0xfff1;
738 symbols [i].st_info = ELF32_ST_INFO (STB_GLOBAL, 0);
740 symbols [i].st_name = str_table_add (strtab, "_edata");
741 symbols [i].st_shndx = 0xfff1;
742 symbols [i].st_info = ELF32_ST_INFO (STB_GLOBAL, 0);
744 symbols [i].st_name = str_table_add (strtab, "_end");
745 symbols [i].st_shndx = 0xfff1;
746 symbols [i].st_info = ELF32_ST_INFO (STB_GLOBAL, 0);
752 /* add to hash table */
755 chain = hash + 2 + hash [0];
756 for (i = 0; i < hash [1]; ++i) {
758 /*g_print ("checking %d '%s' (sym %d)\n", symbols [i].st_name, strtab->data->str + symbols [i].st_name, i);*/
759 if (!symbols [i].st_name)
761 hashc = elf_hash ((guint8*)strtab->data->str + symbols [i].st_name);
762 slot = hashc % hash [0];
763 /*g_print ("hashing '%s' at slot %d (sym %d)\n", strtab->data->str + symbols [i].st_name, slot, i);*/
765 chain [i] = bucket [slot];
776 reloc_symbols (MonoImageWriter *acfg, ElfSymbol *symbols, ElfSectHeader *sheaders, ElfStrTable *strtab, gboolean dynamic)
784 for (section = acfg->sections; section; section = section->next) {
787 symbols [i].st_value = sheaders [section->shidx].sh_addr;
791 for (i = 1; i < SECT_NUM; ++i) {
792 symbols [i].st_value = sheaders [i].sh_addr;
795 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
798 if (dynamic && !symbol->is_global)
800 section = symbol->section;
801 lab = g_hash_table_lookup (acfg->labels, symbol->name);
802 offset = lab->offset;
803 if (section->parent) {
804 symbols [i].st_value = sheaders [section->parent->shidx].sh_addr + section->cur_offset + offset;
806 symbols [i].st_value = sheaders [section->shidx].sh_addr + offset;
811 symbols [i].st_value = sheaders [SECT_BSS].sh_addr;
814 symbols [i].st_value = sheaders [SECT_DATA].sh_addr + sheaders [SECT_DATA].sh_size;
817 symbols [i].st_value = sheaders [SECT_BSS].sh_addr + sheaders [SECT_BSS].sh_size;
822 resolve_reloc (MonoImageWriter *acfg, BinReloc *reloc, guint8 **out_data, gsize *out_vaddr, gsize *out_start_val, gsize *out_end_val)
825 gssize end_val, start_val;
828 end_val = get_label_addr (acfg, reloc->val1);
830 start_val = get_label_addr (acfg, reloc->val2);
831 } else if (reloc->val2_section) {
832 start_val = reloc->val2_offset;
833 if (reloc->val2_section->parent)
834 start_val += reloc->val2_section->parent->virt_offset + reloc->val2_section->cur_offset;
836 start_val += reloc->val2_section->virt_offset;
840 end_val = end_val - start_val + reloc->offset;
841 if (reloc->section->parent) {
842 data = reloc->section->parent->data;
843 data += reloc->section->cur_offset;
844 data += reloc->section_offset;
845 vaddr = reloc->section->parent->virt_offset;
846 vaddr += reloc->section->cur_offset;
847 vaddr += reloc->section_offset;
849 data = reloc->section->data;
850 data += reloc->section_offset;
851 vaddr = reloc->section->virt_offset;
852 vaddr += reloc->section_offset;
855 *out_start_val = start_val;
856 *out_end_val = end_val;
864 resolve_relocations (MonoImageWriter *acfg)
868 gsize end_val, start_val;
873 rr = g_new0 (ElfRelocA, acfg->num_relocs);
876 for (reloc = acfg->relocations; reloc; reloc = reloc->next) {
877 resolve_reloc (acfg, reloc, &data, &vaddr, &start_val, &end_val);
878 /* FIXME: little endian */
880 data [1] = end_val >> 8;
881 data [2] = end_val >> 16;
882 data [3] = end_val >> 24;
884 if (start_val == 0 && reloc->val1 [0] != '.') {
885 rr [i].r_offset = vaddr;
886 rr [i].r_info = R_X86_64_RELATIVE;
887 rr [i].r_addend = end_val;
889 g_assert (i <= acfg->num_relocs);
895 #else /* USE_ELF_RELA */
898 do_reloc (MonoImageWriter *acfg, BinReloc *reloc, guint8 *data, gssize addr)
902 * We use the official ARM relocation types, but implement only the stuff actually
903 * needed by the code we generate.
905 switch (reloc->reloc_type) {
908 guint32 *code = (guint32*)(gpointer)data;
912 if (reloc->reloc_type == R_ARM_CALL)
914 g_assert (data [3] == 0xeb);
917 g_assert (data [3] == 0xea);
918 if (diff >= 0 && diff <= 33554431) {
920 ins = (ins & 0xff000000) | diff;
922 } else if (diff <= 0 && diff >= -33554432) {
924 ins = (ins & 0xff000000) | (diff & ~0xff000000);
927 g_assert_not_reached ();
931 case R_ARM_ALU_PC_G0_NC: {
932 /* Generated by emit_plt () */
936 g_assert (val <= 0xffffff);
938 ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_PC, (val & 0xFF0000) >> 16, 16);
940 ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_PC, 0, 0);
941 ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_IP, (val & 0xFF00) >> 8, 24);
942 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, val & 0xFF);
946 g_assert_not_reached ();
949 g_assert_not_reached ();
954 resolve_relocations (MonoImageWriter *acfg)
958 gsize end_val, start_val;
963 rr = g_new0 (ElfReloc, acfg->num_relocs);
966 for (reloc = acfg->relocations; reloc; reloc = reloc->next) {
967 resolve_reloc (acfg, reloc, &data, &vaddr, &start_val, &end_val);
968 /* FIXME: little endian */
969 if (reloc->reloc_type) {
971 g_assert (start_val > 0);
972 do_reloc (acfg, reloc, data, end_val);
975 data [1] = end_val >> 8;
976 data [2] = end_val >> 16;
977 data [3] = end_val >> 24;
980 if (start_val == 0 && reloc->val1 [0] != '.') {
981 rr [i].r_offset = vaddr;
982 rr [i].r_info = R_386_RELATIVE;
984 g_assert (i <= acfg->num_relocs);
990 #endif /* USE_ELF_RELA */
993 bin_writer_emit_writeout (MonoImageWriter *acfg)
997 ElfProgHeader progh [3];
998 ElfSectHeader secth [SECT_NUM];
1004 ElfStrTable str_table = {NULL, NULL};
1005 ElfStrTable sh_str_table = {NULL, NULL};
1006 ElfStrTable dyn_str_table = {NULL, NULL};
1007 BinSection* all_sections [32];
1008 BinSection* sections [SECT_NUM];
1011 ElfDynamic dynamic [14];
1013 int i, num_sections, file_offset, virt_offset, size, num_symtab;
1018 /* Section headers */
1019 memset (§h, 0, sizeof (secth));
1020 memset (&dynamic, 0, sizeof (dynamic));
1021 memset (&header, 0, sizeof (header));
1023 for (i = 1; i < SECT_NUM; ++i) {
1024 secth [i].sh_name = str_table_add (&sh_str_table, section_info [i].name);
1025 secth [i].sh_type = section_info [i].type;
1026 secth [i].sh_addralign = section_info [i].align;
1027 secth [i].sh_flags = section_info [i].flags;
1028 secth [i].sh_entsize = section_info [i].esize;
1030 secth [SECT_DYNSYM].sh_info = SIZEOF_VOID_P == 4 ? 4 : 2;
1031 secth [SECT_SYMTAB].sh_info = SIZEOF_VOID_P == 4 ? 20 : 17;
1032 secth [SECT_HASH].sh_link = SECT_DYNSYM;
1033 secth [SECT_DYNSYM].sh_link = SECT_DYNSTR;
1034 secth [SECT_REL_DYN].sh_link = SECT_DYNSYM;
1035 secth [SECT_RELA_DYN].sh_link = SECT_DYNSYM;
1036 secth [SECT_DYNAMIC].sh_link = SECT_DYNSTR;
1037 secth [SECT_SYMTAB].sh_link = SECT_STRTAB;
1039 num_sections = collect_sections (acfg, secth, all_sections, 16);
1040 hash = build_hash (acfg, num_sections, &dyn_str_table);
1041 num_symtab = hash [1]; /* FIXME */
1043 g_print ("num_sections: %d\n", num_sections);
1044 g_print ("dynsym: %d, dynstr size: %d\n", hash [1], (int)dyn_str_table.data->len);
1045 for (i = 0; i < num_sections; ++i) {
1046 g_print ("section %s, size: %d, %x\n", all_sections [i]->name, all_sections [i]->cur_offset, all_sections [i]->cur_offset);
1049 /* Associate the bin sections with the ELF sections */
1050 memset (sections, 0, sizeof (sections));
1051 for (i = 0; i < num_sections; ++i) {
1052 BinSection *sect = all_sections [i];
1055 for (j = 0; j < SECT_NUM; ++j) {
1056 if (strcmp (sect->name, section_info [j].name) == 0) {
1062 sections [all_sections [i]->shidx] = sect;
1065 /* at this point we know where in the file the first segment sections go */
1066 dynsym = collect_syms (acfg, hash, &dyn_str_table, NULL, NULL);
1067 num_local_syms = hash [1];
1068 symtab = collect_syms (acfg, NULL, &str_table, secth, &num_local_syms);
1070 file_offset = virt_offset = sizeof (header) + sizeof (progh);
1071 secth [SECT_HASH].sh_addr = secth [SECT_HASH].sh_offset = file_offset;
1072 size = sizeof (int) * (2 + hash [0] + hash [1]);
1073 virt_offset = (file_offset += size);
1074 secth [SECT_HASH].sh_size = size;
1075 secth [SECT_DYNSYM].sh_addr = secth [SECT_DYNSYM].sh_offset = file_offset;
1076 size = sizeof (ElfSymbol) * hash [1];
1077 virt_offset = (file_offset += size);
1078 secth [SECT_DYNSYM].sh_size = size;
1079 secth [SECT_DYNSTR].sh_addr = secth [SECT_DYNSTR].sh_offset = file_offset;
1080 size = dyn_str_table.data->len;
1081 virt_offset = (file_offset += size);
1082 secth [SECT_DYNSTR].sh_size = size;
1084 file_offset &= ~(4-1);
1085 secth [SECT_REL_DYN].sh_addr = secth [SECT_REL_DYN].sh_offset = file_offset;
1086 #ifndef USE_ELF_RELA
1087 size = sizeof (ElfReloc) * acfg->num_relocs;
1091 virt_offset = (file_offset += size);
1092 secth [SECT_REL_DYN].sh_size = size;
1093 secth [SECT_RELA_DYN].sh_addr = secth [SECT_RELA_DYN].sh_offset = file_offset;
1095 size = sizeof (ElfRelocA) * acfg->num_relocs;
1099 virt_offset = (file_offset += size);
1100 secth [SECT_RELA_DYN].sh_size = size;
1102 file_offset = ALIGN_TO (file_offset, secth [SECT_TEXT].sh_addralign);
1103 virt_offset = file_offset;
1104 secth [SECT_TEXT].sh_addr = secth [SECT_TEXT].sh_offset = file_offset;
1105 if (sections [SECT_TEXT]) {
1106 size = sections [SECT_TEXT]->cur_offset;
1107 secth [SECT_TEXT].sh_size = size;
1108 file_offset += size;
1111 file_offset = ALIGN_TO (file_offset, secth [SECT_DYNAMIC].sh_addralign);
1112 virt_offset = file_offset;
1114 /* .dynamic, .got.plt, .data, .bss here */
1115 /* Have to increase the virt offset since these go to a separate segment */
1116 virt_offset += PAGESIZE;
1117 secth [SECT_DYNAMIC].sh_addr = virt_offset;
1118 secth [SECT_DYNAMIC].sh_offset = file_offset;
1119 size = sizeof (dynamic);
1120 secth [SECT_DYNAMIC].sh_size = size;
1121 file_offset += size;
1122 virt_offset += size;
1124 file_offset = ALIGN_TO (file_offset, secth [SECT_GOT_PLT].sh_addralign);
1125 virt_offset = ALIGN_TO (virt_offset, secth [SECT_GOT_PLT].sh_addralign);
1126 secth [SECT_GOT_PLT].sh_addr = virt_offset;
1127 secth [SECT_GOT_PLT].sh_offset = file_offset;
1129 secth [SECT_GOT_PLT].sh_size = size;
1130 file_offset += size;
1131 virt_offset += size;
1133 file_offset = ALIGN_TO (file_offset, secth [SECT_DATA].sh_addralign);
1134 virt_offset = ALIGN_TO (virt_offset, secth [SECT_DATA].sh_addralign);
1135 secth [SECT_DATA].sh_addr = virt_offset;
1136 secth [SECT_DATA].sh_offset = file_offset;
1137 if (sections [SECT_DATA]) {
1138 size = sections [SECT_DATA]->cur_offset;
1139 secth [SECT_DATA].sh_size = size;
1140 file_offset += size;
1141 virt_offset += size;
1144 file_offset = ALIGN_TO (file_offset, secth [SECT_BSS].sh_addralign);
1145 virt_offset = ALIGN_TO (virt_offset, secth [SECT_BSS].sh_addralign);
1146 secth [SECT_BSS].sh_addr = virt_offset;
1147 secth [SECT_BSS].sh_offset = file_offset;
1148 if (sections [SECT_BSS]) {
1149 size = sections [SECT_BSS]->cur_offset;
1150 secth [SECT_BSS].sh_size = size;
1153 /* virtual doesn't matter anymore */
1154 file_offset = ALIGN_TO (file_offset, secth [SECT_DEBUG_FRAME].sh_addralign);
1155 secth [SECT_DEBUG_FRAME].sh_offset = file_offset;
1156 if (sections [SECT_DEBUG_FRAME])
1157 size = sections [SECT_DEBUG_FRAME]->cur_offset;
1160 secth [SECT_DEBUG_FRAME].sh_size = size;
1161 file_offset += size;
1163 secth [SECT_DEBUG_INFO].sh_offset = file_offset;
1164 if (sections [SECT_DEBUG_INFO])
1165 size = sections [SECT_DEBUG_INFO]->cur_offset;
1168 secth [SECT_DEBUG_INFO].sh_size = size;
1169 file_offset += size;
1171 secth [SECT_DEBUG_ABBREV].sh_offset = file_offset;
1172 if (sections [SECT_DEBUG_ABBREV])
1173 size = sections [SECT_DEBUG_ABBREV]->cur_offset;
1176 secth [SECT_DEBUG_ABBREV].sh_size = size;
1177 file_offset += size;
1179 secth [SECT_DEBUG_LINE].sh_offset = file_offset;
1180 if (sections [SECT_DEBUG_LINE])
1181 size = sections [SECT_DEBUG_LINE]->cur_offset;
1184 secth [SECT_DEBUG_LINE].sh_size = size;
1185 file_offset += size;
1187 secth [SECT_DEBUG_LOC].sh_offset = file_offset;
1188 if (sections [SECT_DEBUG_LOC])
1189 size = sections [SECT_DEBUG_LOC]->cur_offset;
1192 secth [SECT_DEBUG_LOC].sh_size = size;
1193 file_offset += size;
1195 file_offset = ALIGN_TO (file_offset, secth [SECT_SHSTRTAB].sh_addralign);
1196 secth [SECT_SHSTRTAB].sh_offset = file_offset;
1197 size = sh_str_table.data->len;
1198 secth [SECT_SHSTRTAB].sh_size = size;
1199 file_offset += size;
1201 file_offset = ALIGN_TO (file_offset, secth [SECT_SYMTAB].sh_addralign);
1202 secth [SECT_SYMTAB].sh_offset = file_offset;
1203 size = sizeof (ElfSymbol) * num_local_syms;
1204 secth [SECT_SYMTAB].sh_size = size;
1205 file_offset += size;
1207 file_offset = ALIGN_TO (file_offset, secth [SECT_STRTAB].sh_addralign);
1208 secth [SECT_STRTAB].sh_offset = file_offset;
1209 size = str_table.data->len;
1210 secth [SECT_STRTAB].sh_size = size;
1211 file_offset += size;
1214 file_offset &= ~(4-1);
1216 header.e_ident [EI_MAG0] = ELFMAG0;
1217 header.e_ident [EI_MAG1] = ELFMAG1;
1218 header.e_ident [EI_MAG2] = ELFMAG2;
1219 header.e_ident [EI_MAG3] = ELFMAG3;
1220 header.e_ident [EI_CLASS] = SIZEOF_VOID_P == 4 ? ELFCLASS32 : ELFCLASS64;
1221 header.e_ident [EI_DATA] = ELFDATA2LSB;
1222 header.e_ident [EI_VERSION] = EV_CURRENT;
1223 header.e_ident [EI_OSABI] = ELFOSABI_NONE;
1224 header.e_ident [EI_ABIVERSION] = 0;
1225 for (i = EI_PAD; i < EI_NIDENT; ++i)
1226 header.e_ident [i] = 0;
1228 header.e_type = ET_DYN;
1229 #if defined(TARGET_X86)
1230 header.e_machine = EM_386;
1231 #elif defined(TARGET_AMD64)
1232 header.e_machine = EM_X86_64;
1233 #elif defined(TARGET_ARM)
1234 header.e_machine = EM_ARM;
1236 g_assert_not_reached ();
1238 header.e_version = 1;
1240 header.e_phoff = sizeof (header);
1241 header.e_ehsize = sizeof (header);
1242 header.e_phentsize = sizeof (ElfProgHeader);
1244 header.e_entry = secth [SECT_TEXT].sh_addr;
1245 header.e_shstrndx = SECT_SHSTRTAB;
1246 header.e_shentsize = sizeof (ElfSectHeader);
1247 header.e_shnum = SECT_NUM;
1248 header.e_shoff = file_offset;
1252 dynamic [i].d_tag = DT_HASH;
1253 dynamic [i].d_un.d_val = secth [SECT_HASH].sh_offset;
1255 dynamic [i].d_tag = DT_STRTAB;
1256 dynamic [i].d_un.d_val = secth [SECT_DYNSTR].sh_offset;
1258 dynamic [i].d_tag = DT_SYMTAB;
1259 dynamic [i].d_un.d_val = secth [SECT_DYNSYM].sh_offset;
1261 dynamic [i].d_tag = DT_STRSZ;
1262 dynamic [i].d_un.d_val = dyn_str_table.data->len;
1264 dynamic [i].d_tag = DT_SYMENT;
1265 dynamic [i].d_un.d_val = sizeof (ElfSymbol);
1268 dynamic [i].d_tag = DT_RELA;
1269 dynamic [i].d_un.d_val = secth [SECT_RELA_DYN].sh_offset;
1271 dynamic [i].d_tag = DT_RELASZ;
1272 dynamic [i].d_un.d_val = secth [SECT_RELA_DYN].sh_size;
1274 dynamic [i].d_tag = DT_RELAENT;
1275 dynamic [i].d_un.d_val = sizeof (ElfRelocA);
1278 dynamic [i].d_tag = DT_REL;
1279 dynamic [i].d_un.d_val = secth [SECT_REL_DYN].sh_offset;
1281 dynamic [i].d_tag = DT_RELSZ;
1282 dynamic [i].d_un.d_val = secth [SECT_REL_DYN].sh_size;
1284 dynamic [i].d_tag = DT_RELENT;
1285 dynamic [i].d_un.d_val = sizeof (ElfReloc);
1288 dynamic [i].d_tag = DT_RELCOUNT;
1289 dynamic [i].d_un.d_val = acfg->num_relocs;
1292 /* Program header */
1293 memset (&progh, 0, sizeof (progh));
1294 progh [0].p_type = PT_LOAD;
1295 progh [0].p_filesz = progh [0].p_memsz = secth [SECT_DYNAMIC].sh_offset;
1296 progh [0].p_align = 4096;
1297 progh [0].p_flags = 5;
1299 progh [1].p_type = PT_LOAD;
1300 progh [1].p_offset = secth [SECT_DYNAMIC].sh_offset;
1301 progh [1].p_vaddr = progh [1].p_paddr = secth [SECT_DYNAMIC].sh_addr;
1302 progh [1].p_filesz = secth [SECT_BSS].sh_offset - secth [SECT_DYNAMIC].sh_offset;
1303 progh [1].p_memsz = secth [SECT_BSS].sh_addr + secth [SECT_BSS].sh_size - secth [SECT_DYNAMIC].sh_addr;
1304 progh [1].p_align = 4096;
1305 progh [1].p_flags = 6;
1307 progh [2].p_type = PT_DYNAMIC;
1308 progh [2].p_offset = secth [SECT_DYNAMIC].sh_offset;
1309 progh [2].p_vaddr = progh [2].p_paddr = secth [SECT_DYNAMIC].sh_addr;
1310 progh [2].p_filesz = progh [2].p_memsz = secth [SECT_DYNAMIC].sh_size;
1311 progh [2].p_align = SIZEOF_VOID_P;
1312 progh [2].p_flags = 6;
1314 /* Compute the addresses of the bin sections, so relocation can be done */
1315 for (i = 0; i < SECT_NUM; ++i) {
1317 sections [i]->file_offset = secth [i].sh_offset;
1318 sections [i]->virt_offset = secth [i].sh_addr;
1322 reloc_symbols (acfg, dynsym, secth, &dyn_str_table, TRUE);
1323 reloc_symbols (acfg, symtab, secth, &str_table, FALSE);
1324 relocs = resolve_relocations (acfg);
1326 fwrite (&header, sizeof (header), 1, file);
1327 fwrite (&progh, sizeof (progh), 1, file);
1328 fwrite (hash, sizeof (int) * (hash [0] + hash [1] + 2), 1, file);
1329 fwrite (dynsym, sizeof (ElfSymbol) * hash [1], 1, file);
1330 fwrite (dyn_str_table.data->str, dyn_str_table.data->len, 1, file);
1332 fseek (file, secth [SECT_REL_DYN].sh_offset, SEEK_SET);
1333 fwrite (relocs, sizeof (ElfReloc), acfg->num_relocs, file);
1336 fseek (file, secth [SECT_RELA_DYN].sh_offset, SEEK_SET);
1337 fwrite (relocs, secth [SECT_RELA_DYN].sh_size, 1, file);
1340 if (sections [SECT_TEXT]) {
1341 fseek (file, secth [SECT_TEXT].sh_offset, SEEK_SET);
1342 fwrite (sections [SECT_TEXT]->data, sections [SECT_TEXT]->cur_offset, 1, file);
1345 fwrite (dynamic, sizeof (dynamic), 1, file);
1348 size = secth [SECT_DYNAMIC].sh_addr;
1349 fwrite (&size, sizeof (size), 1, file);
1352 if (sections [SECT_DATA]) {
1353 fseek (file, secth [SECT_DATA].sh_offset, SEEK_SET);
1354 fwrite (sections [SECT_DATA]->data, sections [SECT_DATA]->cur_offset, 1, file);
1357 fseek (file, secth [SECT_DEBUG_FRAME].sh_offset, SEEK_SET);
1358 if (sections [SECT_DEBUG_FRAME])
1359 fwrite (sections [SECT_DEBUG_FRAME]->data, sections [SECT_DEBUG_FRAME]->cur_offset, 1, file);
1360 fseek (file, secth [SECT_DEBUG_INFO].sh_offset, SEEK_SET);
1361 if (sections [SECT_DEBUG_INFO])
1362 fwrite (sections [SECT_DEBUG_INFO]->data, sections [SECT_DEBUG_INFO]->cur_offset, 1, file);
1363 fseek (file, secth [SECT_DEBUG_ABBREV].sh_offset, SEEK_SET);
1364 if (sections [SECT_DEBUG_ABBREV])
1365 fwrite (sections [SECT_DEBUG_ABBREV]->data, sections [SECT_DEBUG_ABBREV]->cur_offset, 1, file);
1366 fseek (file, secth [SECT_DEBUG_LINE].sh_offset, SEEK_SET);
1367 if (sections [SECT_DEBUG_LINE])
1368 fwrite (sections [SECT_DEBUG_LINE]->data, sections [SECT_DEBUG_LINE]->cur_offset, 1, file);
1369 fseek (file, secth [SECT_DEBUG_LINE].sh_offset, SEEK_SET);
1370 if (sections [SECT_DEBUG_LOC])
1371 fwrite (sections [SECT_DEBUG_LOC]->data, sections [SECT_DEBUG_LOC]->cur_offset, 1, file);
1372 fseek (file, secth [SECT_SHSTRTAB].sh_offset, SEEK_SET);
1373 fwrite (sh_str_table.data->str, sh_str_table.data->len, 1, file);
1374 fseek (file, secth [SECT_SYMTAB].sh_offset, SEEK_SET);
1375 fwrite (symtab, sizeof (ElfSymbol) * num_local_syms, 1, file);
1376 fseek (file, secth [SECT_STRTAB].sh_offset, SEEK_SET);
1377 fwrite (str_table.data->str, str_table.data->len, 1, file);
1378 /*g_print ("file_offset %d vs %d\n", file_offset, ftell (file));*/
1379 /*g_assert (file_offset >= ftell (file));*/
1380 fseek (file, file_offset, SEEK_SET);
1381 fwrite (§h, sizeof (secth), 1, file);
1387 #endif /* USE_ELF_WRITER */
1389 #endif /* USE_BIN_WRITER */
1394 asm_writer_emit_start (MonoImageWriter *acfg)
1399 asm_writer_emit_writeout (MonoImageWriter *acfg)
1407 asm_writer_emit_unset_mode (MonoImageWriter *acfg)
1409 if (acfg->mode == EMIT_NONE)
1411 fprintf (acfg->fp, "\n");
1412 acfg->mode = EMIT_NONE;
1416 asm_writer_emit_section_change (MonoImageWriter *acfg, const char *section_name, int subsection_index)
1418 asm_writer_emit_unset_mode (acfg);
1419 #if defined(PLATFORM_WIN32)
1420 fprintf (acfg->fp, ".section %s\n", section_name);
1421 #elif defined(TARGET_ASM_APPLE)
1422 if (strcmp(section_name, ".bss") == 0)
1423 fprintf (acfg->fp, "%s\n", ".data");
1425 fprintf (acfg->fp, "%s\n", section_name);
1426 #elif defined(TARGET_ARM) || defined(TARGET_POWERPC)
1427 /* ARM gas doesn't seem to like subsections of .bss */
1428 if (!strcmp (section_name, ".text") || !strcmp (section_name, ".data")) {
1429 fprintf (acfg->fp, "%s %d\n", section_name, subsection_index);
1431 fprintf (acfg->fp, ".section \"%s\"\n", section_name);
1432 fprintf (acfg->fp, ".subsection %d\n", subsection_index);
1435 if (!strcmp (section_name, ".text") || !strcmp (section_name, ".data") || !strcmp (section_name, ".bss")) {
1436 fprintf (acfg->fp, "%s %d\n", section_name, subsection_index);
1438 fprintf (acfg->fp, ".section \"%s\"\n", section_name);
1439 fprintf (acfg->fp, ".subsection %d\n", subsection_index);
1445 asm_writer_emit_symbol_type (MonoImageWriter *acfg, const char *name, gboolean func)
1454 asm_writer_emit_unset_mode (acfg);
1455 #if defined(TARGET_ASM_APPLE)
1457 #elif defined(TARGET_ARM)
1458 fprintf (acfg->fp, "\t.type %s,#%s\n", name, stype);
1459 #elif defined(PLATFORM_WIN32)
1462 fprintf (acfg->fp, "\t.type %s,@%s\n", name, stype);
1467 asm_writer_emit_global (MonoImageWriter *acfg, const char *name, gboolean func)
1469 asm_writer_emit_unset_mode (acfg);
1470 #if (defined(__ppc__) && defined(TARGET_ASM_APPLE)) || defined(PLATFORM_WIN32)
1471 // mach-o always uses a '_' prefix.
1472 fprintf (acfg->fp, "\t.globl _%s\n", name);
1474 fprintf (acfg->fp, "\t.globl %s\n", name);
1477 asm_writer_emit_symbol_type (acfg, name, func);
1481 asm_writer_emit_local_symbol (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean func)
1483 asm_writer_emit_unset_mode (acfg);
1485 #ifndef TARGET_ASM_APPLE
1486 fprintf (acfg->fp, "\t.local %s\n", name);
1489 asm_writer_emit_symbol_type (acfg, name, func);
1493 asm_writer_emit_label (MonoImageWriter *acfg, const char *name)
1495 asm_writer_emit_unset_mode (acfg);
1496 #if (defined(__ppc__) && defined(TARGET_ASM_APPLE)) || defined(PLATFORM_WIN32)
1497 // mach-o always uses a '_' prefix.
1498 fprintf (acfg->fp, "_%s:\n", name);
1500 fprintf (acfg->fp, "%s:\n", name);
1503 #if defined(PLATFORM_WIN32)
1504 /* Emit a normal label too */
1505 fprintf (acfg->fp, "%s:\n", name);
1510 asm_writer_emit_string (MonoImageWriter *acfg, const char *value)
1512 asm_writer_emit_unset_mode (acfg);
1513 fprintf (acfg->fp, "\t%s \"%s\"\n", AS_STRING_DIRECTIVE, value);
1517 asm_writer_emit_line (MonoImageWriter *acfg)
1519 asm_writer_emit_unset_mode (acfg);
1520 fprintf (acfg->fp, "\n");
1524 asm_writer_emit_alignment (MonoImageWriter *acfg, int size)
1526 asm_writer_emit_unset_mode (acfg);
1527 #if defined(TARGET_ARM)
1528 fprintf (acfg->fp, "\t.align %d\n", ilog2 (size));
1529 #elif defined(__ppc__) && defined(TARGET_ASM_APPLE)
1530 // the mach-o assembler specifies alignments as powers of 2.
1531 fprintf (acfg->fp, "\t.align %d\t; ilog2\n", ilog2(size));
1532 #elif defined(TARGET_ASM_GAS)
1533 fprintf (acfg->fp, "\t.balign %d\n", size);
1535 fprintf (acfg->fp, "\t.align %d\n", size);
1540 asm_writer_emit_pointer_unaligned (MonoImageWriter *acfg, const char *target)
1542 asm_writer_emit_unset_mode (acfg);
1543 fprintf (acfg->fp, "\t%s %s\n", AS_POINTER_DIRECTIVE, target ? target : "0");
1547 asm_writer_emit_pointer (MonoImageWriter *acfg, const char *target)
1549 asm_writer_emit_unset_mode (acfg);
1550 asm_writer_emit_alignment (acfg, sizeof (gpointer));
1551 asm_writer_emit_pointer_unaligned (acfg, target);
1554 static char *byte_to_str;
1557 asm_writer_emit_bytes (MonoImageWriter *acfg, const guint8* buf, int size)
1560 if (acfg->mode != EMIT_BYTE) {
1561 acfg->mode = EMIT_BYTE;
1562 acfg->col_count = 0;
1565 if (byte_to_str == NULL) {
1566 byte_to_str = g_new0 (char, 256 * 8);
1567 for (i = 0; i < 256; ++i) {
1568 sprintf (byte_to_str + (i * 8), ",%d", i);
1572 for (i = 0; i < size; ++i, ++acfg->col_count) {
1573 if ((acfg->col_count % 32) == 0)
1574 fprintf (acfg->fp, "\n\t.byte %d", buf [i]);
1576 fputs (byte_to_str + (buf [i] * 8), acfg->fp);
1581 asm_writer_emit_int16 (MonoImageWriter *acfg, int value)
1583 if (acfg->mode != EMIT_WORD) {
1584 acfg->mode = EMIT_WORD;
1585 acfg->col_count = 0;
1587 if ((acfg->col_count++ % 8) == 0)
1588 fprintf (acfg->fp, "\n\t%s ", AS_INT16_DIRECTIVE);
1590 fprintf (acfg->fp, ", ");
1591 fprintf (acfg->fp, "%d", value);
1595 asm_writer_emit_int32 (MonoImageWriter *acfg, int value)
1597 if (acfg->mode != EMIT_LONG) {
1598 acfg->mode = EMIT_LONG;
1599 acfg->col_count = 0;
1601 if ((acfg->col_count++ % 8) == 0)
1602 fprintf (acfg->fp, "\n\t%s ", AS_INT32_DIRECTIVE);
1604 fprintf (acfg->fp, ",");
1605 fprintf (acfg->fp, "%d", value);
1609 asm_writer_emit_symbol_diff (MonoImageWriter *acfg, const char *end, const char* start, int offset)
1611 if (acfg->mode != EMIT_LONG) {
1612 acfg->mode = EMIT_LONG;
1613 acfg->col_count = 0;
1615 if ((acfg->col_count++ % 8) == 0)
1616 fprintf (acfg->fp, "\n\t%s ", AS_INT32_DIRECTIVE);
1618 fprintf (acfg->fp, ",");
1620 fprintf (acfg->fp, "%s - %s + %d", end, start, offset);
1621 else if (offset < 0)
1622 fprintf (acfg->fp, "%s - %s %d", end, start, offset);
1624 fprintf (acfg->fp, "%s - %s", end, start);
1628 asm_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
1630 asm_writer_emit_unset_mode (acfg);
1631 fprintf (acfg->fp, "\t%s %d\n", AS_SKIP_DIRECTIVE, num);
1634 /* EMIT FUNCTIONS */
1637 img_writer_emit_start (MonoImageWriter *acfg)
1639 #ifdef USE_BIN_WRITER
1640 if (acfg->use_bin_writer)
1641 bin_writer_emit_start (acfg);
1643 asm_writer_emit_start (acfg);
1645 asm_writer_emit_start (acfg);
1650 img_writer_emit_section_change (MonoImageWriter *acfg, const char *section_name, int subsection_index)
1652 #ifdef USE_BIN_WRITER
1653 if (acfg->use_bin_writer)
1654 bin_writer_emit_section_change (acfg, section_name, subsection_index);
1656 asm_writer_emit_section_change (acfg, section_name, subsection_index);
1658 asm_writer_emit_section_change (acfg, section_name, subsection_index);
1661 acfg->current_section = section_name;
1662 acfg->current_subsection = subsection_index;
1666 img_writer_emit_push_section (MonoImageWriter *acfg, const char *section_name, int subsection)
1668 g_assert (acfg->stack_pos < 16 - 1);
1669 acfg->section_stack [acfg->stack_pos] = acfg->current_section;
1670 acfg->subsection_stack [acfg->stack_pos] = acfg->current_subsection;
1673 img_writer_emit_section_change (acfg, section_name, subsection);
1677 img_writer_emit_pop_section (MonoImageWriter *acfg)
1679 g_assert (acfg->stack_pos > 0);
1681 img_writer_emit_section_change (acfg, acfg->section_stack [acfg->stack_pos], acfg->subsection_stack [acfg->stack_pos]);
1685 img_writer_emit_global (MonoImageWriter *acfg, const char *name, gboolean func)
1687 #ifdef USE_BIN_WRITER
1688 if (acfg->use_bin_writer)
1689 bin_writer_emit_global (acfg, name, func);
1691 asm_writer_emit_global (acfg, name, func);
1693 asm_writer_emit_global (acfg, name, func);
1698 img_writer_emit_local_symbol (MonoImageWriter *acfg, const char *name, const char *end_label, gboolean func)
1700 #ifdef USE_BIN_WRITER
1701 if (acfg->use_bin_writer)
1702 bin_writer_emit_local_symbol (acfg, name, end_label, func);
1704 asm_writer_emit_local_symbol (acfg, name, end_label, func);
1706 asm_writer_emit_local_symbol (acfg, name, end_label, func);
1711 img_writer_emit_label (MonoImageWriter *acfg, const char *name)
1713 #ifdef USE_BIN_WRITER
1714 if (acfg->use_bin_writer)
1715 bin_writer_emit_label (acfg, name);
1717 asm_writer_emit_label (acfg, name);
1719 asm_writer_emit_label (acfg, name);
1724 img_writer_emit_bytes (MonoImageWriter *acfg, const guint8* buf, int size)
1726 #ifdef USE_BIN_WRITER
1727 if (acfg->use_bin_writer)
1728 bin_writer_emit_bytes (acfg, buf, size);
1730 asm_writer_emit_bytes (acfg, buf, size);
1732 asm_writer_emit_bytes (acfg, buf, size);
1737 img_writer_emit_string (MonoImageWriter *acfg, const char *value)
1739 #ifdef USE_BIN_WRITER
1740 if (acfg->use_bin_writer)
1741 bin_writer_emit_string (acfg, value);
1743 asm_writer_emit_string (acfg, value);
1745 asm_writer_emit_string (acfg, value);
1750 img_writer_emit_line (MonoImageWriter *acfg)
1752 #ifdef USE_BIN_WRITER
1753 if (acfg->use_bin_writer)
1754 bin_writer_emit_line (acfg);
1756 asm_writer_emit_line (acfg);
1758 asm_writer_emit_line (acfg);
1763 img_writer_emit_alignment (MonoImageWriter *acfg, int size)
1765 #ifdef USE_BIN_WRITER
1766 if (acfg->use_bin_writer)
1767 bin_writer_emit_alignment (acfg, size);
1769 asm_writer_emit_alignment (acfg, size);
1771 asm_writer_emit_alignment (acfg, size);
1776 img_writer_emit_pointer_unaligned (MonoImageWriter *acfg, const char *target)
1778 #ifdef USE_BIN_WRITER
1779 if (acfg->use_bin_writer)
1780 bin_writer_emit_pointer_unaligned (acfg, target);
1782 asm_writer_emit_pointer_unaligned (acfg, target);
1784 asm_writer_emit_pointer_unaligned (acfg, target);
1789 img_writer_emit_pointer (MonoImageWriter *acfg, const char *target)
1791 #ifdef USE_BIN_WRITER
1792 if (acfg->use_bin_writer)
1793 bin_writer_emit_pointer (acfg, target);
1795 asm_writer_emit_pointer (acfg, target);
1797 asm_writer_emit_pointer (acfg, target);
1802 img_writer_emit_int16 (MonoImageWriter *acfg, int value)
1804 #ifdef USE_BIN_WRITER
1805 if (acfg->use_bin_writer)
1806 bin_writer_emit_int16 (acfg, value);
1808 asm_writer_emit_int16 (acfg, value);
1810 asm_writer_emit_int16 (acfg, value);
1815 img_writer_emit_int32 (MonoImageWriter *acfg, int value)
1817 #ifdef USE_BIN_WRITER
1818 if (acfg->use_bin_writer)
1819 bin_writer_emit_int32 (acfg, value);
1821 asm_writer_emit_int32 (acfg, value);
1823 asm_writer_emit_int32 (acfg, value);
1828 img_writer_emit_symbol_diff (MonoImageWriter *acfg, const char *end, const char* start, int offset)
1830 #ifdef USE_BIN_WRITER
1831 if (acfg->use_bin_writer)
1832 bin_writer_emit_symbol_diff (acfg, end, start, offset);
1834 asm_writer_emit_symbol_diff (acfg, end, start, offset);
1836 asm_writer_emit_symbol_diff (acfg, end, start, offset);
1841 img_writer_emit_zero_bytes (MonoImageWriter *acfg, int num)
1843 #ifdef USE_BIN_WRITER
1844 if (acfg->use_bin_writer)
1845 bin_writer_emit_zero_bytes (acfg, num);
1847 asm_writer_emit_zero_bytes (acfg, num);
1849 asm_writer_emit_zero_bytes (acfg, num);
1854 img_writer_emit_writeout (MonoImageWriter *acfg)
1856 #ifdef USE_BIN_WRITER
1857 if (acfg->use_bin_writer)
1858 return bin_writer_emit_writeout (acfg);
1860 return asm_writer_emit_writeout (acfg);
1862 return asm_writer_emit_writeout (acfg);
1867 img_writer_emit_byte (MonoImageWriter *acfg, guint8 val)
1869 img_writer_emit_bytes (acfg, &val, 1);
1873 * Emit a relocation entry of type RELOC_TYPE against symbol SYMBOL at the current PC.
1874 * Do not advance PC.
1877 img_writer_emit_reloc (MonoImageWriter *acfg, int reloc_type, const char *symbol, int addend)
1879 /* This is only supported by the bin writer */
1880 #ifdef USE_BIN_WRITER
1881 if (acfg->use_bin_writer)
1882 bin_writer_emit_reloc (acfg, reloc_type, symbol, addend);
1884 g_assert_not_reached ();
1886 g_assert_not_reached ();
1891 * img_writer_emit_unset_mode:
1893 * Flush buffered data so it is safe to write to the output file from outside this
1894 * module. This is a nop for the binary writer.
1897 img_writer_emit_unset_mode (MonoImageWriter *acfg)
1899 if (!acfg->use_bin_writer)
1900 asm_writer_emit_unset_mode (acfg);
1904 * Return whenever the binary writer is supported on this platform.
1907 bin_writer_supported (void)
1909 #ifdef USE_BIN_WRITER
1917 img_writer_create (FILE *fp, gboolean use_bin_writer)
1919 MonoImageWriter *w = g_new0 (MonoImageWriter, 1);
1921 #ifndef USE_BIN_WRITER
1922 g_assert (!use_bin_writer);
1926 w->use_bin_writer = use_bin_writer;
1927 w->mempool = mono_mempool_new ();
1933 img_writer_destroy (MonoImageWriter *w)
1935 // FIXME: Free all the stuff
1936 mono_mempool_destroy (w->mempool);