2 * image-writer.c: Creation of object files or assembly files using the same interface.
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Zoltan Varga (vargaz@gmail.com)
7 * Paolo Molaro (lupus@ximian.com)
9 * (C) 2002 Ximian, Inc.
13 #include <sys/types.h>
32 #include <limits.h> /* for PAGESIZE */
37 #include "image-writer.h"
40 #include <mono/utils/freebsd-elf32.h>
41 #include <mono/utils/freebsd-elf64.h>
46 #define TV_DECLARE(name) gint64 name
47 #define TV_GETTIME(tv) tv = mono_100ns_ticks ()
48 #define TV_ELAPSED(start,end) (((end) - (start)) / 10)
51 * The used assembler dialect
52 * TARGET_ASM_APPLE == apple assembler on OSX
53 * TARGET_ASM_GAS == GNU assembler
55 #if !defined(TARGET_ASM_APPLE) && !defined(TARGET_ASM_GAS)
56 #if defined(TARGET_MACH) && !defined(__native_client_codegen__)
57 #define TARGET_ASM_APPLE
59 #define TARGET_ASM_GAS
64 * Defines for the directives used by different assemblers
66 #if defined(TARGET_POWERPC) || defined(TARGET_MACH)
67 #define AS_STRING_DIRECTIVE ".asciz"
69 #define AS_STRING_DIRECTIVE ".string"
72 #define AS_INT32_DIRECTIVE ".long"
73 #define AS_INT64_DIRECTIVE ".quad"
75 #if (defined(TARGET_AMD64) || defined(TARGET_POWERPC64)) && !defined(__mono_ilp32__)
76 #define AS_POINTER_DIRECTIVE ".quad"
77 #elif defined(TARGET_ARM64)
79 #ifdef TARGET_ASM_APPLE
80 #define AS_POINTER_DIRECTIVE ".quad"
82 #define AS_POINTER_DIRECTIVE ".xword"
86 #define AS_POINTER_DIRECTIVE ".long"
89 #if defined(TARGET_ASM_APPLE)
90 #define AS_INT16_DIRECTIVE ".short"
91 #elif defined(TARGET_ASM_GAS)
92 #define AS_INT16_DIRECTIVE ".hword"
94 #define AS_INT16_DIRECTIVE ".word"
97 #if defined(TARGET_ASM_APPLE)
98 #define AS_SKIP_DIRECTIVE ".space"
100 #define AS_SKIP_DIRECTIVE ".skip"
103 #if defined(TARGET_ASM_APPLE)
104 #define AS_GLOBAL_PREFIX "_"
106 #define AS_GLOBAL_PREFIX ""
109 #ifdef TARGET_ASM_APPLE
110 #define AS_TEMP_LABEL_PREFIX "L"
112 #define AS_TEMP_LABEL_PREFIX ".L"
115 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
116 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
117 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
119 #if defined(TARGET_AMD64) && !defined(HOST_WIN32) && !defined(__APPLE__)
120 #define USE_ELF_WRITER 1
121 #define USE_ELF_RELA 1
124 #if defined(TARGET_X86) && !defined(HOST_WIN32) && !defined(__APPLE__)
125 #define USE_ELF_WRITER 1
128 #if defined(TARGET_ARM) && !defined(TARGET_MACH) && !defined(HOST_WIN32)
129 //#define USE_ELF_WRITER 1
132 #if defined(__mips__)
133 #define USE_ELF_WRITER 1
136 #if defined(TARGET_X86) && defined(__APPLE__)
137 //#define USE_MACH_WRITER
140 #if defined(USE_ELF_WRITER) || defined(USE_MACH_WRITER)
141 #define USE_BIN_WRITER 1
144 #ifdef USE_BIN_WRITER
146 typedef struct _BinSymbol BinSymbol
;
147 typedef struct _BinReloc BinReloc
;
148 typedef struct _BinSection BinSection
;
160 struct _MonoImageWriter
{
161 MonoMemPool
*mempool
;
163 gboolean use_bin_writer
;
164 const char *current_section
;
165 int current_subsection
;
166 const char *section_stack
[16];
167 int subsection_stack
[16];
171 #ifdef USE_BIN_WRITER
173 BinSection
*sections
;
174 BinSection
*cur_section
;
175 BinReloc
*relocations
;
179 int out_buf_size
, out_buf_pos
;
183 int mode
; /* emit mode */
184 int col_count
; /* bytes emitted per .byte line */
188 static G_GNUC_UNUSED
int
189 ilog2(register int value
)
192 while (value
& ~0xf) count
+= 4, value
>>= 4;
193 while (value
) count
++, value
>>= 1;
197 #ifdef USE_BIN_WRITER
199 typedef struct _BinLabel BinLabel
;
210 BinSection
*val2_section
;
223 gboolean is_function
;
244 bin_writer_emit_start (MonoImageWriter
*acfg
)
246 acfg
->labels
= g_hash_table_new (g_str_hash
, g_str_equal
);
250 bin_writer_emit_section_change (MonoImageWriter
*acfg
, const char *section_name
, int subsection_index
)
254 if (acfg
->cur_section
&& acfg
->cur_section
->subsection
== subsection_index
255 && strcmp (acfg
->cur_section
->name
, section_name
) == 0)
257 for (section
= acfg
->sections
; section
; section
= section
->next
) {
258 if (section
->subsection
== subsection_index
&& strcmp (section
->name
, section_name
) == 0) {
259 acfg
->cur_section
= section
;
264 section
= g_new0 (BinSection
, 1);
265 section
->name
= g_strdup (section_name
);
266 section
->subsection
= subsection_index
;
267 section
->next
= acfg
->sections
;
268 acfg
->sections
= section
;
269 acfg
->cur_section
= section
;
274 bin_writer_set_section_addr (MonoImageWriter
*acfg
, guint64 addr
)
276 acfg
->cur_section
->addr
= addr
;
277 acfg
->cur_section
->has_addr
= TRUE
;
281 bin_writer_emit_symbol_inner (MonoImageWriter
*acfg
, const char *name
, const char *end_label
, gboolean is_global
, gboolean func
)
283 BinSymbol
*symbol
= g_new0 (BinSymbol
, 1);
284 symbol
->name
= g_strdup (name
);
286 symbol
->end_label
= g_strdup (end_label
);
287 symbol
->is_function
= func
;
288 symbol
->is_global
= is_global
;
289 symbol
->section
= acfg
->cur_section
;
290 /* FIXME: we align after this call... */
291 symbol
->offset
= symbol
->section
->cur_offset
;
292 symbol
->next
= acfg
->symbols
;
293 acfg
->symbols
= symbol
;
297 bin_writer_emit_global (MonoImageWriter
*acfg
, const char *name
, gboolean func
)
299 bin_writer_emit_symbol_inner (acfg
, name
, NULL
, TRUE
, func
);
303 bin_writer_emit_local_symbol (MonoImageWriter
*acfg
, const char *name
, const char *end_label
, gboolean func
)
305 bin_writer_emit_symbol_inner (acfg
, name
, end_label
, FALSE
, func
);
309 bin_writer_emit_label (MonoImageWriter
*acfg
, const char *name
)
311 BinLabel
*label
= g_new0 (BinLabel
, 1);
312 label
->name
= g_strdup (name
);
313 label
->section
= acfg
->cur_section
;
314 label
->offset
= acfg
->cur_section
->cur_offset
;
315 g_hash_table_insert (acfg
->labels
, label
->name
, label
);
319 bin_writer_emit_ensure_buffer (BinSection
*section
, int size
)
321 int new_offset
= section
->cur_offset
+ size
;
322 if (new_offset
>= section
->data_len
) {
323 int new_size
= section
->data_len
? section
->data_len
* 2: 256;
325 while (new_size
<= new_offset
)
327 data
= (guint8
*)g_malloc0 (new_size
);
328 #ifdef __native_client_codegen__
329 /* for Native Client, fill empty space with HLT instruction */
331 memset(data
, 0xf4, new_size
);
333 memcpy (data
, section
->data
, section
->data_len
);
334 g_free (section
->data
);
335 section
->data
= data
;
336 section
->data_len
= new_size
;
341 bin_writer_emit_bytes (MonoImageWriter
*acfg
, const guint8
* buf
, int size
)
343 bin_writer_emit_ensure_buffer (acfg
->cur_section
, size
);
344 memcpy (acfg
->cur_section
->data
+ acfg
->cur_section
->cur_offset
, buf
, size
);
345 acfg
->cur_section
->cur_offset
+= size
;
349 bin_writer_emit_string (MonoImageWriter
*acfg
, const char *value
)
351 int size
= strlen (value
) + 1;
352 bin_writer_emit_bytes (acfg
, (const guint8
*)value
, size
);
356 bin_writer_emit_line (MonoImageWriter
*acfg
)
358 /* Nothing to do in binary writer */
362 bin_writer_emit_alignment (MonoImageWriter
*acfg
, int size
)
364 int offset
= acfg
->cur_section
->cur_offset
;
366 offset
+= (size
- 1);
367 offset
&= ~(size
- 1);
368 add
= offset
- acfg
->cur_section
->cur_offset
;
370 bin_writer_emit_ensure_buffer (acfg
->cur_section
, add
);
371 acfg
->cur_section
->cur_offset
+= add
;
375 #ifdef __native_client_codegen__
377 bin_writer_emit_nacl_call_alignment (MonoImageWriter
*acfg
) {
378 int offset
= acfg
->cur_section
->cur_offset
;
379 int padding
= kNaClAlignment
- (offset
& kNaClAlignmentMask
) - kNaClLengthOfCallImm
;
380 guint8 padc
= '\x90';
382 if (padding
< 0) padding
+= kNaClAlignment
;
384 while (padding
> 0) {
385 bin_writer_emit_bytes(acfg
, &padc
, 1);
389 #endif /* __native_client_codegen__ */
392 bin_writer_emit_pointer_unaligned (MonoImageWriter
*acfg
, const char *target
)
397 acfg
->cur_section
->cur_offset
+= sizeof (gpointer
);
401 reloc
= g_new0 (BinReloc
, 1);
402 reloc
->val1
= g_strdup (target
);
403 reloc
->section
= acfg
->cur_section
;
404 reloc
->section_offset
= acfg
->cur_section
->cur_offset
;
405 reloc
->next
= acfg
->relocations
;
406 acfg
->relocations
= reloc
;
407 if (strcmp (reloc
->section
->name
, ".data") == 0) {
409 //g_print ("reloc: %s at %d\n", target, acfg->cur_section->cur_offset);
411 acfg
->cur_section
->cur_offset
+= sizeof (gpointer
);
415 bin_writer_emit_pointer (MonoImageWriter
*acfg
, const char *target
)
417 bin_writer_emit_alignment (acfg
, sizeof (gpointer
));
418 bin_writer_emit_pointer_unaligned (acfg
, target
);
422 bin_writer_emit_int16 (MonoImageWriter
*acfg
, int value
)
425 bin_writer_emit_ensure_buffer (acfg
->cur_section
, 2);
426 data
= acfg
->cur_section
->data
+ acfg
->cur_section
->cur_offset
;
427 acfg
->cur_section
->cur_offset
+= 2;
428 /* FIXME: little endian */
430 data
[1] = value
>> 8;
434 bin_writer_emit_int32 (MonoImageWriter
*acfg
, int value
)
437 bin_writer_emit_ensure_buffer (acfg
->cur_section
, 4);
438 data
= acfg
->cur_section
->data
+ acfg
->cur_section
->cur_offset
;
439 acfg
->cur_section
->cur_offset
+= 4;
440 /* FIXME: little endian */
442 data
[1] = value
>> 8;
443 data
[2] = value
>> 16;
444 data
[3] = value
>> 24;
448 create_reloc (MonoImageWriter
*acfg
, const char *end
, const char* start
, int offset
)
451 reloc
= (BinReloc
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (BinReloc
));
452 reloc
->val1
= mono_mempool_strdup (acfg
->mempool
, end
);
453 if (strcmp (start
, ".") == 0) {
454 reloc
->val2_section
= acfg
->cur_section
;
455 reloc
->val2_offset
= acfg
->cur_section
->cur_offset
;
457 reloc
->val2
= mono_mempool_strdup (acfg
->mempool
, start
);
459 reloc
->offset
= offset
;
460 reloc
->section
= acfg
->cur_section
;
461 reloc
->section_offset
= acfg
->cur_section
->cur_offset
;
462 reloc
->next
= acfg
->relocations
;
463 acfg
->relocations
= reloc
;
468 bin_writer_emit_symbol_diff (MonoImageWriter
*acfg
, const char *end
, const char* start
, int offset
)
470 create_reloc (acfg
, end
, start
, offset
);
471 acfg
->cur_section
->cur_offset
+= 4;
472 /*if (strcmp (reloc->section->name, ".data") == 0) {
474 g_print ("reloc: %s - %s + %d at %d\n", end, start, offset, acfg->cur_section->cur_offset - 4);
479 * Emit a relocation entry of type RELOC_TYPE against symbol SYMBOL at the current PC.
482 static G_GNUC_UNUSED
void
483 bin_writer_emit_reloc (MonoImageWriter
*acfg
, int reloc_type
, const char *symbol
, int addend
)
485 BinReloc
*reloc
= create_reloc (acfg
, symbol
, ".", addend
);
486 reloc
->reloc_type
= reloc_type
;
490 bin_writer_emit_zero_bytes (MonoImageWriter
*acfg
, int num
)
492 bin_writer_emit_ensure_buffer (acfg
->cur_section
, num
);
493 acfg
->cur_section
->cur_offset
+= num
;
497 bin_writer_fwrite (MonoImageWriter
*acfg
, void *val
, size_t size
, size_t nmemb
)
500 fwrite (val
, size
, nmemb
, acfg
->fp
);
502 g_assert (acfg
->out_buf_pos
+ (size
* nmemb
) <= acfg
->out_buf_size
);
503 memcpy (acfg
->out_buf
+ acfg
->out_buf_pos
, val
, size
* nmemb
);
504 acfg
->out_buf_pos
+= (size
* nmemb
);
509 bin_writer_fseek (MonoImageWriter
*acfg
, int offset
)
512 fseek (acfg
->fp
, offset
, SEEK_SET
);
514 acfg
->out_buf_pos
= offset
;
517 #ifdef USE_MACH_WRITER
520 * This is a minimal implementation designed to support xdebug on 32 bit osx
521 * FIXME: 64 bit support
524 #include <mach-o/loader.h>
527 get_label_addr (MonoImageWriter
*acfg
, const char *name
)
534 lab
= g_hash_table_lookup (acfg
->labels
, name
);
536 g_error ("Undefined label: '%s'.\n", name
);
537 section
= lab
->section
;
538 offset
= lab
->offset
;
539 if (section
->parent
) {
540 value
= section
->parent
->virt_offset
+ section
->cur_offset
+ offset
;
542 value
= section
->virt_offset
+ offset
;
549 resolve_reloc (MonoImageWriter
*acfg
, BinReloc
*reloc
, guint8
**out_data
, gsize
*out_vaddr
, gsize
*out_start_val
, gsize
*out_end_val
)
552 gssize end_val
, start_val
;
555 end_val
= get_label_addr (acfg
, reloc
->val1
);
557 start_val
= get_label_addr (acfg
, reloc
->val2
);
558 } else if (reloc
->val2_section
) {
559 start_val
= reloc
->val2_offset
;
560 if (reloc
->val2_section
->parent
)
561 start_val
+= reloc
->val2_section
->parent
->virt_offset
+ reloc
->val2_section
->cur_offset
;
563 start_val
+= reloc
->val2_section
->virt_offset
;
567 end_val
= end_val
- start_val
+ reloc
->offset
;
568 if (reloc
->section
->parent
) {
569 data
= reloc
->section
->parent
->data
;
570 data
+= reloc
->section
->cur_offset
;
571 data
+= reloc
->section_offset
;
572 vaddr
= reloc
->section
->parent
->virt_offset
;
573 vaddr
+= reloc
->section
->cur_offset
;
574 vaddr
+= reloc
->section_offset
;
576 data
= reloc
->section
->data
;
577 data
+= reloc
->section_offset
;
578 vaddr
= reloc
->section
->virt_offset
;
579 vaddr
+= reloc
->section_offset
;
582 *out_start_val
= start_val
;
583 *out_end_val
= end_val
;
589 resolve_relocations (MonoImageWriter
*acfg
)
593 gsize end_val
, start_val
;
596 /* Only resolve static relocations */
597 for (reloc
= acfg
->relocations
; reloc
; reloc
= reloc
->next
) {
598 resolve_reloc (acfg
, reloc
, &data
, &vaddr
, &start_val
, &end_val
);
600 data
[1] = end_val
>> 8;
601 data
[2] = end_val
>> 16;
602 data
[3] = end_val
>> 24;
607 bin_writer_emit_writeout (MonoImageWriter
*acfg
)
610 int sindex
, file_size
, nsections
, file_offset
, vmaddr
;
611 struct mach_header header
;
612 struct segment_command segment
;
613 struct section
*sections
;
615 /* Assing vm addresses to sections */
618 for (s
= acfg
->sections
; s
; s
= s
->next
) {
619 s
->virt_offset
= vmaddr
;
620 vmaddr
+= s
->cur_offset
;
624 resolve_relocations (acfg
);
628 memset (&header
, 0, sizeof (header
));
629 header
.magic
= MH_MAGIC
;
630 header
.cputype
= CPU_TYPE_X86
;
631 header
.cpusubtype
= CPU_SUBTYPE_X86_ALL
;
632 header
.filetype
= MH_OBJECT
;
634 header
.sizeofcmds
= 0;
637 file_offset
+= sizeof (header
);
639 memset (&segment
, 0, sizeof (segment
));
640 segment
.cmd
= LC_SEGMENT
;
641 segment
.cmdsize
= sizeof (segment
);
642 segment
.maxprot
= VM_PROT_READ
|VM_PROT_WRITE
|VM_PROT_EXECUTE
;
643 segment
.initprot
= VM_PROT_READ
|VM_PROT_WRITE
|VM_PROT_EXECUTE
;
645 file_offset
+= sizeof (segment
);
646 file_offset
+= nsections
* sizeof (struct section
);
648 sections
= g_new0 (struct section
, nsections
);
650 for (s
= acfg
->sections
; s
; s
= s
->next
) {
651 s
->file_offset
= file_offset
;
653 /* .debug_line -> __debug_line */
654 sprintf (sections
[sindex
].sectname
, "__%s", s
->name
+ 1);
655 sprintf (sections
[sindex
].segname
, "%s", "__DWARF");
656 sections
[sindex
].addr
= s
->virt_offset
;
657 sections
[sindex
].size
= s
->cur_offset
;
658 sections
[sindex
].offset
= s
->file_offset
;
660 file_offset
+= s
->cur_offset
;
663 segment
.cmdsize
+= sizeof (struct section
);
669 header
.sizeofcmds
+= segment
.cmdsize
;
672 file_size
= file_offset
;
675 acfg
->out_buf_size
= file_size
;
676 acfg
->out_buf
= g_malloc (acfg
->out_buf_size
);
679 bin_writer_fwrite (acfg
, &header
, sizeof (header
), 1);
680 bin_writer_fwrite (acfg
, &segment
, sizeof (segment
), 1);
681 bin_writer_fwrite (acfg
, sections
, sizeof (struct section
), nsections
);
682 for (s
= acfg
->sections
; s
; s
= s
->next
) {
684 g_assert (acfg
->out_buf_pos
== s
->file_offset
);
685 bin_writer_fwrite (acfg
, s
->data
, s
->cur_offset
, 1);
696 #ifdef USE_ELF_WRITER
722 #if SIZEOF_VOID_P == 4
724 typedef Elf32_Ehdr ElfHeader
;
725 typedef Elf32_Shdr ElfSectHeader
;
726 typedef Elf32_Phdr ElfProgHeader
;
727 typedef Elf32_Sym ElfSymbol
;
728 typedef Elf32_Rel ElfReloc
;
729 typedef Elf32_Rela ElfRelocA
;
730 typedef Elf32_Dyn ElfDynamic
;
734 typedef Elf64_Ehdr ElfHeader
;
735 typedef Elf64_Shdr ElfSectHeader
;
736 typedef Elf64_Phdr ElfProgHeader
;
737 typedef Elf64_Sym ElfSymbol
;
738 typedef Elf64_Rel ElfReloc
;
739 typedef Elf64_Rela ElfRelocA
;
740 typedef Elf64_Dyn ElfDynamic
;
752 static SectInfo section_info
[] = {
754 {".hash", SHT_HASH
, 4, 2, SIZEOF_VOID_P
},
755 {".dynsym", SHT_DYNSYM
, sizeof (ElfSymbol
), 2, SIZEOF_VOID_P
},
756 {".dynstr", SHT_STRTAB
, 0, 2, 1},
757 {".rel.dyn", SHT_REL
, sizeof (ElfReloc
), 2, SIZEOF_VOID_P
},
758 {".rela.dyn", SHT_RELA
, sizeof (ElfRelocA
), 2, SIZEOF_VOID_P
},
759 {".text", SHT_PROGBITS
, 0, 6, 4096},
760 {".rodata", SHT_PROGBITS
, 0, SHF_ALLOC
, 4096},
761 {".dynamic", SHT_DYNAMIC
, sizeof (ElfDynamic
), 3, SIZEOF_VOID_P
},
762 {".got.plt", SHT_PROGBITS
, SIZEOF_VOID_P
, 3, SIZEOF_VOID_P
},
763 {".data", SHT_PROGBITS
, 0, 3, 8},
764 {".bss", SHT_NOBITS
, 0, 3, 8},
765 {".debug_frame", SHT_PROGBITS
, 0, 0, 8},
766 {".debug_info", SHT_PROGBITS
, 0, 0, 1},
767 {".debug_abbrev", SHT_PROGBITS
, 0, 0, 1},
768 {".debug_line", SHT_PROGBITS
, 0, 0, 1},
769 {".debug_loc", SHT_PROGBITS
, 0, 0, 1},
770 {".shstrtab", SHT_STRTAB
, 0, 0, 1},
771 {".symtab", SHT_SYMTAB
, sizeof (ElfSymbol
), 0, SIZEOF_VOID_P
},
772 {".strtab", SHT_STRTAB
, 0, 0, 1}
781 str_table_add (ElfStrTable
*table
, const char* value
)
785 table
->data
= g_string_new_len ("", 1);
786 table
->hash
= g_hash_table_new (g_str_hash
, g_str_equal
);
788 idx
= GPOINTER_TO_UINT (g_hash_table_lookup (table
->hash
, value
));
791 idx
= table
->data
->len
;
792 g_string_append (table
->data
, value
);
793 g_string_append_c (table
->data
, 0);
794 g_hash_table_insert (table
->hash
, (void*)value
, GUINT_TO_POINTER (idx
));
799 append_subsection (MonoImageWriter
*acfg
, ElfSectHeader
*sheaders
, BinSection
*sect
, BinSection
*add
)
801 int offset
= sect
->cur_offset
;
802 /*offset += (sheaders [sect->shidx].sh_addralign - 1);
803 offset &= ~(sheaders [sect->shidx].sh_addralign - 1);*/
805 * FIXME: we shouldn't align subsections at all, but if we don't then the
806 * stuff inside the subsections which is aligned won't get aligned.
808 if (strcmp (sect
->name
, ".debug_line") != 0) {
812 bin_writer_emit_ensure_buffer (sect
, offset
);
813 //g_print ("section %s aligned to %d from %d\n", sect->name, offset, sect->cur_offset);
814 sect
->cur_offset
= offset
;
816 bin_writer_emit_ensure_buffer (sect
, add
->cur_offset
);
817 memcpy (sect
->data
+ sect
->cur_offset
, add
->data
, add
->cur_offset
);
819 sect
->cur_offset
+= add
->cur_offset
;
820 add
->cur_offset
= offset
; /* it becomes the offset in the parent section */
821 //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);
826 /* merge the subsections */
828 collect_sections (MonoImageWriter
*acfg
, ElfSectHeader
*sheaders
, BinSection
**out
, int num
)
830 int i
, j
, maxs
, num_sections
;
835 for (sect
= acfg
->sections
; sect
; sect
= sect
->next
) {
836 if (sect
->subsection
== 0) {
837 out
[num_sections
++] = sect
;
838 g_assert (num_sections
< num
);
840 maxs
= MAX (maxs
, sect
->subsection
);
842 for (i
= 0; i
< num_sections
; i
++) {
843 for (j
= 1; j
<= maxs
; ++j
) {
844 for (sect
= acfg
->sections
; sect
; sect
= sect
->next
) {
845 if (sect
->subsection
== j
&& strcmp (out
[i
]->name
, sect
->name
) == 0) {
846 append_subsection (acfg
, sheaders
, out
[i
], sect
);
855 elf_hash (const unsigned char *name
)
857 unsigned long h
= 0, g
;
859 h
= (h
<< 4) + *name
++;
860 if ((g
= h
& 0xf0000000))
867 #define NUM_BUCKETS 17
870 build_hash (MonoImageWriter
*acfg
, int num_sections
, ElfStrTable
*dynstr
)
873 int num_symbols
= 1 + num_sections
+ 3;
876 for (symbol
= acfg
->symbols
; symbol
; symbol
= symbol
->next
) {
877 if (!symbol
->is_global
)
880 str_table_add (dynstr
, symbol
->name
);
881 /*g_print ("adding sym: %s\n", symbol->name);*/
883 str_table_add (dynstr
, "__bss_start");
884 str_table_add (dynstr
, "_edata");
885 str_table_add (dynstr
, "_end");
887 data
= g_new0 (int, num_symbols
+ 2 + NUM_BUCKETS
);
888 data
[0] = NUM_BUCKETS
;
889 data
[1] = num_symbols
;
895 get_label_addr (MonoImageWriter
*acfg
, const char *name
)
902 lab
= (BinLabel
*)g_hash_table_lookup (acfg
->labels
, name
);
904 g_error ("Undefined label: '%s'.\n", name
);
905 section
= lab
->section
;
906 offset
= lab
->offset
;
907 if (section
->parent
) {
908 value
= section
->parent
->virt_offset
+ section
->cur_offset
+ offset
;
910 value
= section
->virt_offset
+ offset
;
916 collect_syms (MonoImageWriter
*acfg
, int *hash
, ElfStrTable
*strtab
, ElfSectHeader
*sheaders
, int *num_syms
)
927 symbols
= g_new0 (ElfSymbol
, hash
[1]);
930 for (symbol
= acfg
->symbols
; symbol
; symbol
= symbol
->next
)
933 symbols
= g_new0 (ElfSymbol
, i
+ SECT_NUM
+ 10); /* FIXME */
936 /* the first symbol is undef, all zeroes */
940 for (j
= 1; j
< SECT_NUM
; ++j
) {
941 symbols
[i
].st_info
= ELF32_ST_INFO (STB_LOCAL
, STT_SECTION
);
942 symbols
[i
].st_shndx
= j
;
943 symbols
[i
].st_value
= sheaders
[j
].sh_addr
;
947 for (section
= acfg
->sections
; section
; section
= section
->next
) {
950 symbols
[i
].st_info
= ELF32_ST_INFO (STB_LOCAL
, STT_SECTION
);
951 if (strcmp (section
->name
, ".text") == 0) {
952 symbols
[i
].st_shndx
= SECT_TEXT
;
953 section
->shidx
= SECT_TEXT
;
954 section
->file_offset
= 4096;
955 symbols
[i
].st_value
= section
->virt_offset
;
956 } else if (strcmp (section
->name
, ".rodata") == 0) {
957 symbols
[i
].st_shndx
= SECT_RODATA
;
958 section
->shidx
= SECT_RODATA
;
959 section
->file_offset
= 4096;
960 symbols
[i
].st_value
= section
->virt_offset
;
961 } else if (strcmp (section
->name
, ".data") == 0) {
962 symbols
[i
].st_shndx
= SECT_DATA
;
963 section
->shidx
= SECT_DATA
;
964 section
->file_offset
= 4096 + 28; /* FIXME */
965 symbols
[i
].st_value
= section
->virt_offset
;
966 } else if (strcmp (section
->name
, ".bss") == 0) {
967 symbols
[i
].st_shndx
= SECT_BSS
;
968 section
->shidx
= SECT_BSS
;
969 section
->file_offset
= 4096 + 28 + 8; /* FIXME */
970 symbols
[i
].st_value
= section
->virt_offset
;
975 for (symbol
= acfg
->symbols
; symbol
; symbol
= symbol
->next
) {
978 if (!symbol
->is_global
&& hash
)
980 symbols
[i
].st_info
= ELF32_ST_INFO (symbol
->is_global
? STB_GLOBAL
: STB_LOCAL
, symbol
->is_function
? STT_FUNC
: STT_OBJECT
);
981 symbols
[i
].st_name
= str_table_add (strtab
, symbol
->name
);
982 /*g_print ("sym name %s tabled to %d\n", symbol->name, symbols [i].st_name);*/
983 section
= symbol
->section
;
984 symbols
[i
].st_shndx
= section
->parent
? section
->parent
->shidx
: section
->shidx
;
985 lab
= (BinLabel
*)g_hash_table_lookup (acfg
->labels
, symbol
->name
);
986 offset
= lab
->offset
;
987 if (section
->parent
) {
988 symbols
[i
].st_value
= section
->parent
->virt_offset
+ section
->cur_offset
+ offset
;
990 symbols
[i
].st_value
= section
->virt_offset
+ offset
;
993 if (symbol
->end_label
) {
994 BinLabel
*elab
= (BinLabel
*)g_hash_table_lookup (acfg
->labels
, symbol
->end_label
);
996 symbols
[i
].st_size
= elab
->offset
- lab
->offset
;
1000 /* add special symbols */
1001 symbols
[i
].st_name
= str_table_add (strtab
, "__bss_start");
1002 symbols
[i
].st_shndx
= 0xfff1;
1003 symbols
[i
].st_info
= ELF32_ST_INFO (STB_GLOBAL
, 0);
1005 symbols
[i
].st_name
= str_table_add (strtab
, "_edata");
1006 symbols
[i
].st_shndx
= 0xfff1;
1007 symbols
[i
].st_info
= ELF32_ST_INFO (STB_GLOBAL
, 0);
1009 symbols
[i
].st_name
= str_table_add (strtab
, "_end");
1010 symbols
[i
].st_shndx
= 0xfff1;
1011 symbols
[i
].st_info
= ELF32_ST_INFO (STB_GLOBAL
, 0);
1017 /* add to hash table */
1020 chain
= hash
+ 2 + hash
[0];
1021 for (i
= 0; i
< hash
[1]; ++i
) {
1023 /*g_print ("checking %d '%s' (sym %d)\n", symbols [i].st_name, strtab->data->str + symbols [i].st_name, i);*/
1024 if (!symbols
[i
].st_name
)
1026 hashc
= elf_hash ((guint8
*)strtab
->data
->str
+ symbols
[i
].st_name
);
1027 slot
= hashc
% hash
[0];
1028 /*g_print ("hashing '%s' at slot %d (sym %d)\n", strtab->data->str + symbols [i].st_name, slot, i);*/
1029 if (bucket
[slot
]) {
1030 chain
[i
] = bucket
[slot
];
1041 reloc_symbols (MonoImageWriter
*acfg
, ElfSymbol
*symbols
, ElfSectHeader
*sheaders
, ElfStrTable
*strtab
, gboolean dynamic
)
1043 BinSection
*section
;
1049 for (section
= acfg
->sections
; section
; section
= section
->next
) {
1050 if (section
->parent
)
1052 symbols
[i
].st_value
= sheaders
[section
->shidx
].sh_addr
;
1056 for (i
= 1; i
< SECT_NUM
; ++i
) {
1057 symbols
[i
].st_value
= sheaders
[i
].sh_addr
;
1060 for (symbol
= acfg
->symbols
; symbol
; symbol
= symbol
->next
) {
1063 if (dynamic
&& !symbol
->is_global
)
1065 section
= symbol
->section
;
1066 lab
= (BinLabel
*)g_hash_table_lookup (acfg
->labels
, symbol
->name
);
1067 offset
= lab
->offset
;
1068 if (section
->parent
) {
1069 symbols
[i
].st_value
= sheaders
[section
->parent
->shidx
].sh_addr
+ section
->cur_offset
+ offset
;
1071 symbols
[i
].st_value
= sheaders
[section
->shidx
].sh_addr
+ offset
;
1076 symbols
[i
].st_value
= sheaders
[SECT_BSS
].sh_addr
;
1079 symbols
[i
].st_value
= sheaders
[SECT_DATA
].sh_addr
+ sheaders
[SECT_DATA
].sh_size
;
1082 symbols
[i
].st_value
= sheaders
[SECT_BSS
].sh_addr
+ sheaders
[SECT_BSS
].sh_size
;
1087 resolve_reloc (MonoImageWriter
*acfg
, BinReloc
*reloc
, guint8
**out_data
, gsize
*out_vaddr
, gsize
*out_start_val
, gsize
*out_end_val
)
1090 gssize end_val
, start_val
;
1093 end_val
= get_label_addr (acfg
, reloc
->val1
);
1095 start_val
= get_label_addr (acfg
, reloc
->val2
);
1096 } else if (reloc
->val2_section
) {
1097 start_val
= reloc
->val2_offset
;
1098 if (reloc
->val2_section
->parent
)
1099 start_val
+= reloc
->val2_section
->parent
->virt_offset
+ reloc
->val2_section
->cur_offset
;
1101 start_val
+= reloc
->val2_section
->virt_offset
;
1105 end_val
= end_val
- start_val
+ reloc
->offset
;
1106 if (reloc
->section
->parent
) {
1107 data
= reloc
->section
->parent
->data
;
1108 data
+= reloc
->section
->cur_offset
;
1109 data
+= reloc
->section_offset
;
1110 vaddr
= reloc
->section
->parent
->virt_offset
;
1111 vaddr
+= reloc
->section
->cur_offset
;
1112 vaddr
+= reloc
->section_offset
;
1114 data
= reloc
->section
->data
;
1115 data
+= reloc
->section_offset
;
1116 vaddr
= reloc
->section
->virt_offset
;
1117 vaddr
+= reloc
->section_offset
;
1120 *out_start_val
= start_val
;
1121 *out_end_val
= end_val
;
1129 resolve_relocations (MonoImageWriter
*acfg
)
1133 gsize end_val
, start_val
;
1138 rr
= g_new0 (ElfRelocA
, acfg
->num_relocs
);
1141 for (reloc
= acfg
->relocations
; reloc
; reloc
= reloc
->next
) {
1142 resolve_reloc (acfg
, reloc
, &data
, &vaddr
, &start_val
, &end_val
);
1143 /* FIXME: little endian */
1145 data
[1] = end_val
>> 8;
1146 data
[2] = end_val
>> 16;
1147 data
[3] = end_val
>> 24;
1149 if (start_val
== 0 && reloc
->val1
[0] != '.') {
1150 rr
[i
].r_offset
= vaddr
;
1151 rr
[i
].r_info
= R_X86_64_RELATIVE
;
1152 rr
[i
].r_addend
= end_val
;
1154 g_assert (i
<= acfg
->num_relocs
);
1160 #else /* USE_ELF_RELA */
1163 do_reloc (MonoImageWriter
*acfg
, BinReloc
*reloc
, guint8
*data
, gssize addr
)
1167 * We use the official ARM relocation types, but implement only the stuff actually
1168 * needed by the code we generate.
1170 switch (reloc
->reloc_type
) {
1172 case R_ARM_JUMP24
: {
1173 guint32
*code
= (guint32
*)(gpointer
)data
;
1174 guint32 ins
= *code
;
1177 if (reloc
->reloc_type
== R_ARM_CALL
)
1179 g_assert (data
[3] == 0xeb);
1182 g_assert (data
[3] == 0xea);
1183 if (diff
>= 0 && diff
<= 33554431) {
1185 ins
= (ins
& 0xff000000) | diff
;
1187 } else if (diff
<= 0 && diff
>= -33554432) {
1189 ins
= (ins
& 0xff000000) | (diff
& ~0xff000000);
1192 g_assert_not_reached ();
1196 case R_ARM_ALU_PC_G0_NC
: {
1197 /* Generated by emit_plt () */
1198 guint8
*code
= data
;
1201 g_assert (val
<= 0xffffff);
1203 ARM_ADD_REG_IMM (code
, ARMREG_IP
, ARMREG_PC
, (val
& 0xFF0000) >> 16, 16);
1205 ARM_ADD_REG_IMM (code
, ARMREG_IP
, ARMREG_PC
, 0, 0);
1206 ARM_ADD_REG_IMM (code
, ARMREG_IP
, ARMREG_IP
, (val
& 0xFF00) >> 8, 24);
1207 ARM_LDR_IMM (code
, ARMREG_PC
, ARMREG_IP
, val
& 0xFF);
1211 g_assert_not_reached ();
1214 g_assert_not_reached ();
1219 resolve_relocations (MonoImageWriter
*acfg
)
1223 gsize end_val
, start_val
;
1228 rr
= g_new0 (ElfReloc
, acfg
->num_relocs
);
1231 for (reloc
= acfg
->relocations
; reloc
; reloc
= reloc
->next
) {
1232 resolve_reloc (acfg
, reloc
, &data
, &vaddr
, &start_val
, &end_val
);
1233 /* FIXME: little endian */
1234 if (reloc
->reloc_type
) {
1235 /* Must be static */
1236 g_assert (start_val
> 0);
1237 do_reloc (acfg
, reloc
, data
, end_val
);
1240 data
[1] = end_val
>> 8;
1241 data
[2] = end_val
>> 16;
1242 data
[3] = end_val
>> 24;
1245 if (start_val
== 0 && reloc
->val1
[0] != '.') {
1246 rr
[i
].r_offset
= vaddr
;
1247 rr
[i
].r_info
= R_386_RELATIVE
;
1249 g_assert (i
<= acfg
->num_relocs
);
1255 #endif /* USE_ELF_RELA */
1257 static int normal_sections
[] = { SECT_DATA
, SECT_DEBUG_FRAME
, SECT_DEBUG_INFO
, SECT_DEBUG_ABBREV
, SECT_DEBUG_LINE
, SECT_DEBUG_LOC
};
1260 bin_writer_emit_writeout (MonoImageWriter
*acfg
)
1263 ElfProgHeader progh
[4];
1264 ElfSectHeader secth
[SECT_NUM
];
1270 ElfStrTable str_table
= {NULL
, NULL
};
1271 ElfStrTable sh_str_table
= {NULL
, NULL
};
1272 ElfStrTable dyn_str_table
= {NULL
, NULL
};
1273 BinSection
* all_sections
[32];
1274 BinSection
* sections
[SECT_NUM
];
1277 ElfDynamic dynamic
[14];
1279 int i
, num_sections
, file_offset
, virt_offset
, size
;
1282 /* Section headers */
1283 memset (§h
, 0, sizeof (secth
));
1284 memset (&dynamic
, 0, sizeof (dynamic
));
1285 memset (&header
, 0, sizeof (header
));
1287 for (i
= 1; i
< SECT_NUM
; ++i
) {
1288 secth
[i
].sh_name
= str_table_add (&sh_str_table
, section_info
[i
].name
);
1289 secth
[i
].sh_type
= section_info
[i
].type
;
1290 secth
[i
].sh_addralign
= section_info
[i
].align
;
1291 secth
[i
].sh_flags
= section_info
[i
].flags
;
1292 secth
[i
].sh_entsize
= section_info
[i
].esize
;
1294 secth
[SECT_DYNSYM
].sh_info
= SIZEOF_VOID_P
== 4 ? 4 : 2;
1295 secth
[SECT_SYMTAB
].sh_info
= SIZEOF_VOID_P
== 4 ? 20 : 17;
1296 secth
[SECT_HASH
].sh_link
= SECT_DYNSYM
;
1297 secth
[SECT_DYNSYM
].sh_link
= SECT_DYNSTR
;
1298 secth
[SECT_REL_DYN
].sh_link
= SECT_DYNSYM
;
1299 secth
[SECT_RELA_DYN
].sh_link
= SECT_DYNSYM
;
1300 secth
[SECT_DYNAMIC
].sh_link
= SECT_DYNSTR
;
1301 secth
[SECT_SYMTAB
].sh_link
= SECT_STRTAB
;
1303 num_sections
= collect_sections (acfg
, secth
, all_sections
, 16);
1304 hash
= build_hash (acfg
, num_sections
, &dyn_str_table
);
1306 g_print ("num_sections: %d\n", num_sections
);
1307 g_print ("dynsym: %d, dynstr size: %d\n", hash
[1], (int)dyn_str_table
.data
->len
);
1308 for (i
= 0; i
< num_sections
; ++i
) {
1309 g_print ("section %s, size: %d, %x\n", all_sections
[i
]->name
, all_sections
[i
]->cur_offset
, all_sections
[i
]->cur_offset
);
1312 /* Associate the bin sections with the ELF sections */
1313 memset (sections
, 0, sizeof (sections
));
1314 for (i
= 0; i
< num_sections
; ++i
) {
1315 BinSection
*sect
= all_sections
[i
];
1318 for (j
= 0; j
< SECT_NUM
; ++j
) {
1319 if (strcmp (sect
->name
, section_info
[j
].name
) == 0) {
1325 sections
[all_sections
[i
]->shidx
] = sect
;
1328 /* at this point we know where in the file the first segment sections go */
1329 dynsym
= collect_syms (acfg
, hash
, &dyn_str_table
, NULL
, NULL
);
1330 num_local_syms
= hash
[1];
1331 symtab
= collect_syms (acfg
, NULL
, &str_table
, secth
, &num_local_syms
);
1333 file_offset
= virt_offset
= sizeof (header
) + sizeof (progh
);
1334 secth
[SECT_HASH
].sh_addr
= secth
[SECT_HASH
].sh_offset
= file_offset
;
1335 size
= sizeof (int) * (2 + hash
[0] + hash
[1]);
1336 virt_offset
= (file_offset
+= size
);
1337 secth
[SECT_HASH
].sh_size
= size
;
1338 secth
[SECT_DYNSYM
].sh_addr
= secth
[SECT_DYNSYM
].sh_offset
= file_offset
;
1339 size
= sizeof (ElfSymbol
) * hash
[1];
1340 virt_offset
= (file_offset
+= size
);
1341 secth
[SECT_DYNSYM
].sh_size
= size
;
1342 secth
[SECT_DYNSTR
].sh_addr
= secth
[SECT_DYNSTR
].sh_offset
= file_offset
;
1343 size
= dyn_str_table
.data
->len
;
1344 virt_offset
= (file_offset
+= size
);
1345 secth
[SECT_DYNSTR
].sh_size
= size
;
1347 file_offset
&= ~(4-1);
1348 secth
[SECT_REL_DYN
].sh_addr
= secth
[SECT_REL_DYN
].sh_offset
= file_offset
;
1349 #ifndef USE_ELF_RELA
1350 size
= sizeof (ElfReloc
) * acfg
->num_relocs
;
1354 virt_offset
= (file_offset
+= size
);
1355 secth
[SECT_REL_DYN
].sh_size
= size
;
1356 secth
[SECT_RELA_DYN
].sh_addr
= secth
[SECT_RELA_DYN
].sh_offset
= file_offset
;
1358 size
= sizeof (ElfRelocA
) * acfg
->num_relocs
;
1362 virt_offset
= (file_offset
+= size
);
1363 secth
[SECT_RELA_DYN
].sh_size
= size
;
1365 file_offset
= ALIGN_TO (file_offset
, secth
[SECT_TEXT
].sh_addralign
);
1366 virt_offset
= file_offset
;
1367 secth
[SECT_TEXT
].sh_addr
= secth
[SECT_TEXT
].sh_offset
= file_offset
;
1368 if (sections
[SECT_TEXT
]) {
1369 if (sections
[SECT_TEXT
]->has_addr
) {
1370 secth
[SECT_TEXT
].sh_addr
= sections
[SECT_TEXT
]->addr
;
1371 secth
[SECT_TEXT
].sh_flags
&= ~SHF_ALLOC
;
1373 size
= sections
[SECT_TEXT
]->cur_offset
;
1374 secth
[SECT_TEXT
].sh_size
= size
;
1375 file_offset
+= size
;
1378 file_offset
= ALIGN_TO (file_offset
, secth
[SECT_RODATA
].sh_addralign
);
1379 virt_offset
= file_offset
;
1380 secth
[SECT_RODATA
].sh_addr
= virt_offset
;
1381 secth
[SECT_RODATA
].sh_offset
= file_offset
;
1382 if (sections
[SECT_RODATA
]) {
1383 size
= sections
[SECT_RODATA
]->cur_offset
;
1384 secth
[SECT_RODATA
].sh_size
= size
;
1385 file_offset
+= size
;
1386 virt_offset
+= size
;
1389 file_offset
= ALIGN_TO (file_offset
, secth
[SECT_DYNAMIC
].sh_addralign
);
1390 virt_offset
= file_offset
;
1392 /* .dynamic, .got.plt, .data, .bss here */
1393 /* Have to increase the virt offset since these go to a separate segment */
1394 virt_offset
+= PAGESIZE
;
1395 secth
[SECT_DYNAMIC
].sh_addr
= virt_offset
;
1396 secth
[SECT_DYNAMIC
].sh_offset
= file_offset
;
1397 size
= sizeof (dynamic
);
1398 secth
[SECT_DYNAMIC
].sh_size
= size
;
1399 file_offset
+= size
;
1400 virt_offset
+= size
;
1402 file_offset
= ALIGN_TO (file_offset
, secth
[SECT_GOT_PLT
].sh_addralign
);
1403 virt_offset
= ALIGN_TO (virt_offset
, secth
[SECT_GOT_PLT
].sh_addralign
);
1404 secth
[SECT_GOT_PLT
].sh_addr
= virt_offset
;
1405 secth
[SECT_GOT_PLT
].sh_offset
= file_offset
;
1406 size
= 3 * SIZEOF_VOID_P
;
1407 secth
[SECT_GOT_PLT
].sh_size
= size
;
1408 file_offset
+= size
;
1409 virt_offset
+= size
;
1411 file_offset
= ALIGN_TO (file_offset
, secth
[SECT_DATA
].sh_addralign
);
1412 virt_offset
= ALIGN_TO (virt_offset
, secth
[SECT_DATA
].sh_addralign
);
1413 secth
[SECT_DATA
].sh_addr
= virt_offset
;
1414 secth
[SECT_DATA
].sh_offset
= file_offset
;
1415 if (sections
[SECT_DATA
]) {
1416 size
= sections
[SECT_DATA
]->cur_offset
;
1417 secth
[SECT_DATA
].sh_size
= size
;
1418 file_offset
+= size
;
1419 virt_offset
+= size
;
1422 file_offset
= ALIGN_TO (file_offset
, secth
[SECT_BSS
].sh_addralign
);
1423 virt_offset
= ALIGN_TO (virt_offset
, secth
[SECT_BSS
].sh_addralign
);
1424 secth
[SECT_BSS
].sh_addr
= virt_offset
;
1425 secth
[SECT_BSS
].sh_offset
= file_offset
;
1426 if (sections
[SECT_BSS
]) {
1427 size
= sections
[SECT_BSS
]->cur_offset
;
1428 secth
[SECT_BSS
].sh_size
= size
;
1431 /* virtual doesn't matter anymore */
1432 file_offset
= ALIGN_TO (file_offset
, secth
[SECT_DEBUG_FRAME
].sh_addralign
);
1433 secth
[SECT_DEBUG_FRAME
].sh_offset
= file_offset
;
1434 if (sections
[SECT_DEBUG_FRAME
])
1435 size
= sections
[SECT_DEBUG_FRAME
]->cur_offset
;
1438 secth
[SECT_DEBUG_FRAME
].sh_size
= size
;
1439 file_offset
+= size
;
1441 secth
[SECT_DEBUG_INFO
].sh_offset
= file_offset
;
1442 if (sections
[SECT_DEBUG_INFO
])
1443 size
= sections
[SECT_DEBUG_INFO
]->cur_offset
;
1446 secth
[SECT_DEBUG_INFO
].sh_size
= size
;
1447 file_offset
+= size
;
1449 secth
[SECT_DEBUG_ABBREV
].sh_offset
= file_offset
;
1450 if (sections
[SECT_DEBUG_ABBREV
])
1451 size
= sections
[SECT_DEBUG_ABBREV
]->cur_offset
;
1454 secth
[SECT_DEBUG_ABBREV
].sh_size
= size
;
1455 file_offset
+= size
;
1457 secth
[SECT_DEBUG_LINE
].sh_offset
= file_offset
;
1458 if (sections
[SECT_DEBUG_LINE
])
1459 size
= sections
[SECT_DEBUG_LINE
]->cur_offset
;
1462 secth
[SECT_DEBUG_LINE
].sh_size
= size
;
1463 file_offset
+= size
;
1465 secth
[SECT_DEBUG_LOC
].sh_offset
= file_offset
;
1466 if (sections
[SECT_DEBUG_LOC
])
1467 size
= sections
[SECT_DEBUG_LOC
]->cur_offset
;
1470 secth
[SECT_DEBUG_LOC
].sh_size
= size
;
1471 file_offset
+= size
;
1473 file_offset
= ALIGN_TO (file_offset
, secth
[SECT_SHSTRTAB
].sh_addralign
);
1474 secth
[SECT_SHSTRTAB
].sh_offset
= file_offset
;
1475 size
= sh_str_table
.data
->len
;
1476 secth
[SECT_SHSTRTAB
].sh_size
= size
;
1477 file_offset
+= size
;
1479 file_offset
= ALIGN_TO (file_offset
, secth
[SECT_SYMTAB
].sh_addralign
);
1480 secth
[SECT_SYMTAB
].sh_offset
= file_offset
;
1481 size
= sizeof (ElfSymbol
) * num_local_syms
;
1482 secth
[SECT_SYMTAB
].sh_size
= size
;
1483 file_offset
+= size
;
1485 file_offset
= ALIGN_TO (file_offset
, secth
[SECT_STRTAB
].sh_addralign
);
1486 secth
[SECT_STRTAB
].sh_offset
= file_offset
;
1487 size
= str_table
.data
->len
;
1488 secth
[SECT_STRTAB
].sh_size
= size
;
1489 file_offset
+= size
;
1491 for (i
= 1; i
< SECT_NUM
; ++i
) {
1492 if (section_info
[i
].esize
!= 0)
1493 g_assert (secth
[i
].sh_size
% section_info
[i
].esize
== 0);
1497 file_offset
&= ~(4-1);
1499 header
.e_ident
[EI_MAG0
] = ELFMAG0
;
1500 header
.e_ident
[EI_MAG1
] = ELFMAG1
;
1501 header
.e_ident
[EI_MAG2
] = ELFMAG2
;
1502 header
.e_ident
[EI_MAG3
] = ELFMAG3
;
1503 header
.e_ident
[EI_CLASS
] = SIZEOF_VOID_P
== 4 ? ELFCLASS32
: ELFCLASS64
;
1504 header
.e_ident
[EI_DATA
] = ELFDATA2LSB
;
1505 header
.e_ident
[EI_VERSION
] = EV_CURRENT
;
1506 header
.e_ident
[EI_OSABI
] = ELFOSABI_NONE
;
1507 header
.e_ident
[EI_ABIVERSION
] = 0;
1508 for (i
= EI_PAD
; i
< EI_NIDENT
; ++i
)
1509 header
.e_ident
[i
] = 0;
1511 header
.e_type
= ET_DYN
;
1512 #if defined(TARGET_X86)
1513 header
.e_machine
= EM_386
;
1514 #elif defined(TARGET_AMD64)
1515 header
.e_machine
= EM_X86_64
;
1516 #elif defined(TARGET_ARM)
1517 header
.e_machine
= EM_ARM
;
1519 g_assert_not_reached ();
1521 header
.e_version
= 1;
1523 header
.e_phoff
= sizeof (header
);
1524 header
.e_ehsize
= sizeof (header
);
1525 header
.e_phentsize
= sizeof (ElfProgHeader
);
1527 header
.e_entry
= secth
[SECT_TEXT
].sh_addr
;
1528 header
.e_shstrndx
= SECT_SHSTRTAB
;
1529 header
.e_shentsize
= sizeof (ElfSectHeader
);
1530 header
.e_shnum
= SECT_NUM
;
1531 header
.e_shoff
= file_offset
;
1535 dynamic
[i
].d_tag
= DT_HASH
;
1536 dynamic
[i
].d_un
.d_val
= secth
[SECT_HASH
].sh_offset
;
1538 dynamic
[i
].d_tag
= DT_STRTAB
;
1539 dynamic
[i
].d_un
.d_val
= secth
[SECT_DYNSTR
].sh_offset
;
1541 dynamic
[i
].d_tag
= DT_SYMTAB
;
1542 dynamic
[i
].d_un
.d_val
= secth
[SECT_DYNSYM
].sh_offset
;
1544 dynamic
[i
].d_tag
= DT_STRSZ
;
1545 dynamic
[i
].d_un
.d_val
= dyn_str_table
.data
->len
;
1547 dynamic
[i
].d_tag
= DT_SYMENT
;
1548 dynamic
[i
].d_un
.d_val
= sizeof (ElfSymbol
);
1551 dynamic
[i
].d_tag
= DT_RELA
;
1552 dynamic
[i
].d_un
.d_val
= secth
[SECT_RELA_DYN
].sh_offset
;
1554 dynamic
[i
].d_tag
= DT_RELASZ
;
1555 dynamic
[i
].d_un
.d_val
= secth
[SECT_RELA_DYN
].sh_size
;
1557 dynamic
[i
].d_tag
= DT_RELAENT
;
1558 dynamic
[i
].d_un
.d_val
= sizeof (ElfRelocA
);
1561 dynamic
[i
].d_tag
= DT_REL
;
1562 dynamic
[i
].d_un
.d_val
= secth
[SECT_REL_DYN
].sh_offset
;
1564 dynamic
[i
].d_tag
= DT_RELSZ
;
1565 dynamic
[i
].d_un
.d_val
= secth
[SECT_REL_DYN
].sh_size
;
1567 dynamic
[i
].d_tag
= DT_RELENT
;
1568 dynamic
[i
].d_un
.d_val
= sizeof (ElfReloc
);
1571 dynamic
[i
].d_tag
= DT_RELCOUNT
;
1572 dynamic
[i
].d_un
.d_val
= acfg
->num_relocs
;
1575 /* Program header */
1576 memset (&progh
, 0, sizeof (progh
));
1577 progh
[0].p_type
= PT_LOAD
;
1578 progh
[0].p_filesz
= progh
[0].p_memsz
= secth
[SECT_DYNAMIC
].sh_offset
;
1579 progh
[0].p_align
= 4096;
1580 progh
[0].p_flags
= 5;
1582 progh
[1].p_type
= PT_LOAD
;
1583 progh
[1].p_offset
= secth
[SECT_DYNAMIC
].sh_offset
;
1584 progh
[1].p_vaddr
= progh
[1].p_paddr
= secth
[SECT_DYNAMIC
].sh_addr
;
1585 progh
[1].p_filesz
= secth
[SECT_BSS
].sh_offset
- secth
[SECT_DYNAMIC
].sh_offset
;
1586 progh
[1].p_memsz
= secth
[SECT_BSS
].sh_addr
+ secth
[SECT_BSS
].sh_size
- secth
[SECT_DYNAMIC
].sh_addr
;
1587 progh
[1].p_align
= 4096;
1588 progh
[1].p_flags
= 6;
1590 progh
[2].p_type
= PT_DYNAMIC
;
1591 progh
[2].p_offset
= secth
[SECT_DYNAMIC
].sh_offset
;
1592 progh
[2].p_vaddr
= progh
[2].p_paddr
= secth
[SECT_DYNAMIC
].sh_addr
;
1593 progh
[2].p_filesz
= progh
[2].p_memsz
= secth
[SECT_DYNAMIC
].sh_size
;
1594 progh
[2].p_align
= SIZEOF_VOID_P
;
1595 progh
[2].p_flags
= 6;
1597 progh
[3].p_type
= PT_GNU_STACK
;
1598 progh
[3].p_offset
= secth
[SECT_DYNAMIC
].sh_offset
;
1599 progh
[3].p_vaddr
= progh
[3].p_paddr
= secth
[SECT_DYNAMIC
].sh_addr
;
1600 progh
[3].p_filesz
= progh
[3].p_memsz
= secth
[SECT_DYNAMIC
].sh_size
;
1601 progh
[3].p_align
= SIZEOF_VOID_P
;
1602 progh
[3].p_flags
= 6;
1604 /* Compute the addresses of the bin sections, so relocation can be done */
1605 for (i
= 0; i
< SECT_NUM
; ++i
) {
1607 sections
[i
]->file_offset
= secth
[i
].sh_offset
;
1608 sections
[i
]->virt_offset
= secth
[i
].sh_addr
;
1612 reloc_symbols (acfg
, dynsym
, secth
, &dyn_str_table
, TRUE
);
1613 reloc_symbols (acfg
, symtab
, secth
, &str_table
, FALSE
);
1614 relocs
= resolve_relocations (acfg
);
1617 acfg
->out_buf_size
= file_offset
+ sizeof (secth
);
1618 acfg
->out_buf
= (guint8
*)g_malloc (acfg
->out_buf_size
);
1621 bin_writer_fwrite (acfg
, &header
, sizeof (header
), 1);
1622 bin_writer_fwrite (acfg
, &progh
, sizeof (progh
), 1);
1623 bin_writer_fwrite (acfg
, hash
, sizeof (int) * (hash
[0] + hash
[1] + 2), 1);
1624 bin_writer_fwrite (acfg
, dynsym
, sizeof (ElfSymbol
) * hash
[1], 1);
1625 bin_writer_fwrite (acfg
, dyn_str_table
.data
->str
, dyn_str_table
.data
->len
, 1);
1627 bin_writer_fseek (acfg
, secth
[SECT_REL_DYN
].sh_offset
);
1628 bin_writer_fwrite (acfg
, relocs
, sizeof (ElfReloc
), acfg
->num_relocs
);
1631 bin_writer_fseek (acfg
, secth
[SECT_RELA_DYN
].sh_offset
);
1632 bin_writer_fwrite (acfg
, relocs
, secth
[SECT_RELA_DYN
].sh_size
, 1);
1635 if (sections
[SECT_TEXT
]) {
1636 bin_writer_fseek (acfg
, secth
[SECT_TEXT
].sh_offset
);
1637 bin_writer_fwrite (acfg
, sections
[SECT_TEXT
]->data
, sections
[SECT_TEXT
]->cur_offset
, 1);
1640 if (sections
[SECT_RODATA
]) {
1641 bin_writer_fseek (acfg
, secth
[SECT_RODATA
].sh_offset
);
1642 bin_writer_fwrite (acfg
, sections
[SECT_RODATA
]->data
, sections
[SECT_RODATA
]->cur_offset
, 1);
1645 bin_writer_fseek (acfg
, secth
[SECT_DYNAMIC
].sh_offset
);
1646 bin_writer_fwrite (acfg
, dynamic
, sizeof (dynamic
), 1);
1649 size
= secth
[SECT_DYNAMIC
].sh_addr
;
1650 bin_writer_fseek (acfg
, secth
[SECT_GOT_PLT
].sh_offset
);
1651 bin_writer_fwrite (acfg
, &size
, sizeof (size
), 1);
1653 /* normal sections */
1654 for (i
= 0; i
< sizeof (normal_sections
) / sizeof (normal_sections
[0]); ++i
) {
1655 int sect
= normal_sections
[i
];
1657 if (sections
[sect
]) {
1658 bin_writer_fseek (acfg
, secth
[sect
].sh_offset
);
1659 bin_writer_fwrite (acfg
, sections
[sect
]->data
, sections
[sect
]->cur_offset
, 1);
1663 bin_writer_fseek (acfg
, secth
[SECT_SHSTRTAB
].sh_offset
);
1664 bin_writer_fwrite (acfg
, sh_str_table
.data
->str
, sh_str_table
.data
->len
, 1);
1665 bin_writer_fseek (acfg
, secth
[SECT_SYMTAB
].sh_offset
);
1666 bin_writer_fwrite (acfg
, symtab
, sizeof (ElfSymbol
) * num_local_syms
, 1);
1667 bin_writer_fseek (acfg
, secth
[SECT_STRTAB
].sh_offset
);
1668 bin_writer_fwrite (acfg
, str_table
.data
->str
, str_table
.data
->len
, 1);
1669 /*g_print ("file_offset %d vs %d\n", file_offset, ftell (file));*/
1670 /*g_assert (file_offset >= ftell (file));*/
1671 bin_writer_fseek (acfg
, file_offset
);
1672 bin_writer_fwrite (acfg
, §h
, sizeof (secth
), 1);
1680 #endif /* USE_ELF_WRITER */
1682 #endif /* USE_BIN_WRITER */
1687 asm_writer_emit_start (MonoImageWriter
*acfg
)
1692 asm_writer_emit_writeout (MonoImageWriter
*acfg
)
1700 asm_writer_emit_unset_mode (MonoImageWriter
*acfg
)
1702 if (acfg
->mode
== EMIT_NONE
)
1704 fprintf (acfg
->fp
, "\n");
1705 acfg
->mode
= EMIT_NONE
;
1709 asm_writer_emit_section_change (MonoImageWriter
*acfg
, const char *section_name
, int subsection_index
)
1711 asm_writer_emit_unset_mode (acfg
);
1712 #if defined(TARGET_ASM_APPLE)
1713 if (strcmp(section_name
, ".bss") == 0)
1714 fprintf (acfg
->fp
, "%s\n", ".data");
1715 else if (strstr (section_name
, ".debug") == section_name
) {
1716 //g_assert (subsection_index == 0);
1717 fprintf (acfg
->fp
, ".section __DWARF, __%s,regular,debug\n", section_name
+ 1);
1719 fprintf (acfg
->fp
, "%s\n", section_name
);
1720 #elif defined(TARGET_ARM) || defined(TARGET_ARM64) || defined(TARGET_POWERPC)
1721 /* ARM gas doesn't seem to like subsections of .bss */
1722 if (!strcmp (section_name
, ".text") || !strcmp (section_name
, ".data")) {
1723 fprintf (acfg
->fp
, "%s %d\n", section_name
, subsection_index
);
1725 fprintf (acfg
->fp
, ".section \"%s\"\n", section_name
);
1726 fprintf (acfg
->fp
, ".subsection %d\n", subsection_index
);
1728 #elif defined(HOST_WIN32)
1729 fprintf (acfg
->fp
, ".section %s\n", section_name
);
1731 if (!strcmp (section_name
, ".text") || !strcmp (section_name
, ".data") || !strcmp (section_name
, ".bss")) {
1732 fprintf (acfg
->fp
, "%s %d\n", section_name
, subsection_index
);
1734 fprintf (acfg
->fp
, ".section \"%s\"\n", section_name
);
1735 fprintf (acfg
->fp
, ".subsection %d\n", subsection_index
);
1741 const char *get_label (const char *s
)
1743 #ifdef TARGET_ASM_APPLE
1744 if (s
[0] == '.' && s
[1] == 'L')
1745 /* apple uses "L" instead of ".L" to mark temporary labels */
1752 asm_writer_emit_symbol_type (MonoImageWriter
*acfg
, const char *name
, gboolean func
)
1761 asm_writer_emit_unset_mode (acfg
);
1762 #if defined(TARGET_ASM_APPLE)
1764 #elif defined(TARGET_ARM)
1765 fprintf (acfg
->fp
, "\t.type %s,#%s\n", name
, stype
);
1767 fprintf (acfg
->fp
, "\t.type %s,@%s\n", name
, stype
);
1772 asm_writer_emit_global (MonoImageWriter
*acfg
, const char *name
, gboolean func
)
1774 asm_writer_emit_unset_mode (acfg
);
1776 fprintf (acfg
->fp
, "\t.globl %s\n", name
);
1778 asm_writer_emit_symbol_type (acfg
, name
, func
);
1782 asm_writer_emit_local_symbol (MonoImageWriter
*acfg
, const char *name
, const char *end_label
, gboolean func
)
1784 asm_writer_emit_unset_mode (acfg
);
1786 #ifndef TARGET_ASM_APPLE
1787 fprintf (acfg
->fp
, "\t.local %s\n", name
);
1790 asm_writer_emit_symbol_type (acfg
, name
, func
);
1794 asm_writer_emit_symbol_size (MonoImageWriter
*acfg
, const char *name
, const char *end_label
)
1796 asm_writer_emit_unset_mode (acfg
);
1798 #ifndef TARGET_ASM_APPLE
1799 fprintf (acfg
->fp
, "\t.size %s,%s-%s\n", name
, end_label
, name
);
1804 asm_writer_emit_label (MonoImageWriter
*acfg
, const char *name
)
1806 asm_writer_emit_unset_mode (acfg
);
1807 fprintf (acfg
->fp
, "%s:\n", get_label (name
));
1811 asm_writer_emit_string (MonoImageWriter
*acfg
, const char *value
)
1813 asm_writer_emit_unset_mode (acfg
);
1814 fprintf (acfg
->fp
, "\t%s \"%s\"\n", AS_STRING_DIRECTIVE
, value
);
1818 asm_writer_emit_line (MonoImageWriter
*acfg
)
1820 asm_writer_emit_unset_mode (acfg
);
1821 fprintf (acfg
->fp
, "\n");
1825 asm_writer_emit_alignment (MonoImageWriter
*acfg
, int size
)
1827 asm_writer_emit_unset_mode (acfg
);
1828 #if defined(TARGET_ARM)
1829 fprintf (acfg
->fp
, "\t.align %d\n", ilog2 (size
));
1830 #elif defined(__ppc__) && defined(TARGET_ASM_APPLE)
1831 // the mach-o assembler specifies alignments as powers of 2.
1832 fprintf (acfg
->fp
, "\t.align %d\t; ilog2\n", ilog2(size
));
1833 #elif defined(TARGET_ASM_GAS)
1834 fprintf (acfg
->fp
, "\t.balign %d\n", size
);
1835 #elif defined(TARGET_ASM_APPLE)
1836 fprintf (acfg
->fp
, "\t.align %d\n", ilog2 (size
));
1838 fprintf (acfg
->fp
, "\t.align %d\n", size
);
1842 #ifndef USE_BIN_WRITER
1844 asm_writer_emit_alignment_fill (MonoImageWriter
*acfg
, int size
, int fill
)
1846 asm_writer_emit_unset_mode (acfg
);
1847 #if defined(TARGET_ASM_APPLE)
1848 fprintf (acfg
->fp
, "\t.align %d, 0x%0x\n", ilog2 (size
), fill
);
1850 asm_writer_emit_alignment (acfg
, size
);
1855 #ifdef __native_client_codegen__
1857 asm_writer_emit_nacl_call_alignment (MonoImageWriter
*acfg
) {
1858 int padding
= kNaClAlignment
- kNaClLengthOfCallImm
;
1859 guint8 padc
= '\x90';
1861 fprintf (acfg
->fp
, "\n\t.align %d", kNaClAlignment
);
1862 while (padding
> 0) {
1863 fprintf (acfg
->fp
, "\n\t.byte %d", padc
);
1867 #endif /* __native_client_codegen__ */
1870 asm_writer_emit_pointer_unaligned (MonoImageWriter
*acfg
, const char *target
)
1872 asm_writer_emit_unset_mode (acfg
);
1873 fprintf (acfg
->fp
, "\t%s %s\n", AS_POINTER_DIRECTIVE
, target
? target
: "0");
1877 asm_writer_emit_pointer (MonoImageWriter
*acfg
, const char *target
)
1879 asm_writer_emit_unset_mode (acfg
);
1880 asm_writer_emit_alignment (acfg
, sizeof (gpointer
));
1881 asm_writer_emit_pointer_unaligned (acfg
, target
);
1884 static char *byte_to_str
;
1887 asm_writer_emit_bytes (MonoImageWriter
*acfg
, const guint8
* buf
, int size
)
1890 if (acfg
->mode
!= EMIT_BYTE
) {
1891 acfg
->mode
= EMIT_BYTE
;
1892 acfg
->col_count
= 0;
1895 if (byte_to_str
== NULL
) {
1896 byte_to_str
= g_new0 (char, 256 * 8);
1897 for (i
= 0; i
< 256; ++i
) {
1898 sprintf (byte_to_str
+ (i
* 8), ",%d", i
);
1902 for (i
= 0; i
< size
; ++i
, ++acfg
->col_count
) {
1903 if ((acfg
->col_count
% 32) == 0)
1904 fprintf (acfg
->fp
, "\n\t.byte %d", buf
[i
]);
1906 fputs (byte_to_str
+ (buf
[i
] * 8), acfg
->fp
);
1911 asm_writer_emit_int16 (MonoImageWriter
*acfg
, int value
)
1913 if (acfg
->mode
!= EMIT_WORD
) {
1914 acfg
->mode
= EMIT_WORD
;
1915 acfg
->col_count
= 0;
1917 if ((acfg
->col_count
++ % 8) == 0)
1918 fprintf (acfg
->fp
, "\n\t%s ", AS_INT16_DIRECTIVE
);
1920 fprintf (acfg
->fp
, ", ");
1921 fprintf (acfg
->fp
, "%d", value
);
1925 asm_writer_emit_int32 (MonoImageWriter
*acfg
, int value
)
1927 if (acfg
->mode
!= EMIT_LONG
) {
1928 acfg
->mode
= EMIT_LONG
;
1929 acfg
->col_count
= 0;
1931 if ((acfg
->col_count
++ % 8) == 0)
1932 fprintf (acfg
->fp
, "\n\t%s ", AS_INT32_DIRECTIVE
);
1934 fprintf (acfg
->fp
, ",");
1935 fprintf (acfg
->fp
, "%d", value
);
1939 asm_writer_emit_symbol_diff (MonoImageWriter
*acfg
, const char *end
, const char* start
, int offset
)
1941 #ifdef TARGET_ASM_APPLE
1942 //char symbol [128];
1945 if (acfg
->mode
!= EMIT_LONG
) {
1946 acfg
->mode
= EMIT_LONG
;
1947 acfg
->col_count
= 0;
1950 // FIXME: This doesn't seem to work on the iphone
1952 //#ifdef TARGET_ASM_APPLE
1953 /* The apple assembler needs a separate symbol to be able to handle complex expressions */
1954 sprintf (symbol
, "LTMP_SYM%d", acfg
->label_gen
);
1955 start
= get_label (start
);
1956 end
= get_label (end
);
1959 fprintf (acfg
->fp
, "\n%s=%s - %s + %d", symbol
, end
, start
, offset
);
1960 else if (offset
< 0)
1961 fprintf (acfg
->fp
, "\n%s=%s - %s %d", symbol
, end
, start
, offset
);
1963 fprintf (acfg
->fp
, "\n%s=%s - %s", symbol
, end
, start
);
1965 fprintf (acfg
->fp
, "\n\t%s ", AS_INT32_DIRECTIVE
);
1966 fprintf (acfg
->fp
, "%s", symbol
);
1968 start
= get_label (start
);
1969 end
= get_label (end
);
1971 if (offset
== 0 && strcmp (start
, ".") != 0) {
1973 sprintf (symbol
, "%sDIFF_SYM%d", AS_TEMP_LABEL_PREFIX
, acfg
->label_gen
);
1975 fprintf (acfg
->fp
, "\n%s=%s - %s", symbol
, end
, start
);
1976 fprintf (acfg
->fp
, "\n\t%s ", AS_INT32_DIRECTIVE
);
1977 fprintf (acfg
->fp
, "%s", symbol
);
1981 if ((acfg
->col_count
++ % 8) == 0)
1982 fprintf (acfg
->fp
, "\n\t%s ", AS_INT32_DIRECTIVE
);
1984 fprintf (acfg
->fp
, ",");
1986 fprintf (acfg
->fp
, "%s - %s + %d", end
, start
, offset
);
1987 else if (offset
< 0)
1988 fprintf (acfg
->fp
, "%s - %s %d", end
, start
, offset
);
1990 fprintf (acfg
->fp
, "%s - %s", end
, start
);
1995 asm_writer_emit_zero_bytes (MonoImageWriter
*acfg
, int num
)
1997 asm_writer_emit_unset_mode (acfg
);
1998 fprintf (acfg
->fp
, "\t%s %d\n", AS_SKIP_DIRECTIVE
, num
);
2001 /* EMIT FUNCTIONS */
2004 mono_img_writer_emit_start (MonoImageWriter
*acfg
)
2006 #ifdef USE_BIN_WRITER
2007 if (acfg
->use_bin_writer
)
2008 bin_writer_emit_start (acfg
);
2010 asm_writer_emit_start (acfg
);
2012 asm_writer_emit_start (acfg
);
2017 mono_img_writer_emit_section_change (MonoImageWriter
*acfg
, const char *section_name
, int subsection_index
)
2019 #ifdef USE_BIN_WRITER
2020 if (acfg
->use_bin_writer
)
2021 bin_writer_emit_section_change (acfg
, section_name
, subsection_index
);
2023 asm_writer_emit_section_change (acfg
, section_name
, subsection_index
);
2025 asm_writer_emit_section_change (acfg
, section_name
, subsection_index
);
2028 acfg
->current_section
= section_name
;
2029 acfg
->current_subsection
= subsection_index
;
2033 mono_img_writer_emit_push_section (MonoImageWriter
*acfg
, const char *section_name
, int subsection
)
2035 g_assert (acfg
->stack_pos
< 16 - 1);
2036 acfg
->section_stack
[acfg
->stack_pos
] = acfg
->current_section
;
2037 acfg
->subsection_stack
[acfg
->stack_pos
] = acfg
->current_subsection
;
2040 mono_img_writer_emit_section_change (acfg
, section_name
, subsection
);
2044 mono_img_writer_emit_pop_section (MonoImageWriter
*acfg
)
2046 g_assert (acfg
->stack_pos
> 0);
2048 mono_img_writer_emit_section_change (acfg
, acfg
->section_stack
[acfg
->stack_pos
], acfg
->subsection_stack
[acfg
->stack_pos
]);
2052 mono_img_writer_set_section_addr (MonoImageWriter
*acfg
, guint64 addr
)
2054 #ifdef USE_BIN_WRITER
2055 if (!acfg
->use_bin_writer
)
2058 bin_writer_set_section_addr (acfg
, addr
);
2065 mono_img_writer_emit_global (MonoImageWriter
*acfg
, const char *name
, gboolean func
)
2067 #ifdef USE_BIN_WRITER
2068 if (acfg
->use_bin_writer
)
2069 bin_writer_emit_global (acfg
, name
, func
);
2071 asm_writer_emit_global (acfg
, name
, func
);
2073 asm_writer_emit_global (acfg
, name
, func
);
2078 mono_img_writer_emit_local_symbol (MonoImageWriter
*acfg
, const char *name
, const char *end_label
, gboolean func
)
2080 #ifdef USE_BIN_WRITER
2081 if (acfg
->use_bin_writer
)
2082 bin_writer_emit_local_symbol (acfg
, name
, end_label
, func
);
2084 asm_writer_emit_local_symbol (acfg
, name
, end_label
, func
);
2086 asm_writer_emit_local_symbol (acfg
, name
, end_label
, func
);
2091 mono_img_writer_emit_symbol_size (MonoImageWriter
*acfg
, const char *name
, const char *end_label
)
2093 if (!acfg
->use_bin_writer
)
2094 asm_writer_emit_symbol_size (acfg
, name
, end_label
);
2098 mono_img_writer_emit_label (MonoImageWriter
*acfg
, const char *name
)
2100 #ifdef USE_BIN_WRITER
2101 if (acfg
->use_bin_writer
)
2102 bin_writer_emit_label (acfg
, name
);
2104 asm_writer_emit_label (acfg
, name
);
2106 asm_writer_emit_label (acfg
, name
);
2111 mono_img_writer_emit_bytes (MonoImageWriter
*acfg
, const guint8
* buf
, int size
)
2113 #ifdef USE_BIN_WRITER
2114 if (acfg
->use_bin_writer
)
2115 bin_writer_emit_bytes (acfg
, buf
, size
);
2117 asm_writer_emit_bytes (acfg
, buf
, size
);
2119 asm_writer_emit_bytes (acfg
, buf
, size
);
2124 mono_img_writer_emit_string (MonoImageWriter
*acfg
, const char *value
)
2126 #ifdef USE_BIN_WRITER
2127 if (acfg
->use_bin_writer
)
2128 bin_writer_emit_string (acfg
, value
);
2130 asm_writer_emit_string (acfg
, value
);
2132 asm_writer_emit_string (acfg
, value
);
2137 mono_img_writer_emit_line (MonoImageWriter
*acfg
)
2139 #ifdef USE_BIN_WRITER
2140 if (acfg
->use_bin_writer
)
2141 bin_writer_emit_line (acfg
);
2143 asm_writer_emit_line (acfg
);
2145 asm_writer_emit_line (acfg
);
2150 mono_img_writer_emit_alignment (MonoImageWriter
*acfg
, int size
)
2152 #ifdef USE_BIN_WRITER
2153 if (acfg
->use_bin_writer
)
2154 bin_writer_emit_alignment (acfg
, size
);
2156 asm_writer_emit_alignment (acfg
, size
);
2158 asm_writer_emit_alignment (acfg
, size
);
2163 mono_img_writer_emit_alignment_fill (MonoImageWriter
*acfg
, int size
, int fill
)
2165 #ifdef USE_BIN_WRITER
2166 if (acfg
->use_bin_writer
)
2167 bin_writer_emit_alignment (acfg
, size
);
2169 asm_writer_emit_alignment (acfg
, size
);
2171 asm_writer_emit_alignment_fill (acfg
, size
, fill
);
2175 #ifdef __native_client_codegen__
2177 mono_img_writer_emit_nacl_call_alignment (MonoImageWriter
*acfg
) {
2178 #ifdef USE_BIN_WRITER
2179 if (acfg
->use_bin_writer
)
2180 bin_writer_emit_nacl_call_alignment (acfg
);
2182 asm_writer_emit_nacl_call_alignment (acfg
);
2184 g_assert_not_reached();
2187 #endif /* __native_client_codegen__ */
2190 mono_img_writer_emit_pointer_unaligned (MonoImageWriter
*acfg
, const char *target
)
2192 #ifdef USE_BIN_WRITER
2193 if (acfg
->use_bin_writer
)
2194 bin_writer_emit_pointer_unaligned (acfg
, target
);
2196 asm_writer_emit_pointer_unaligned (acfg
, target
);
2198 asm_writer_emit_pointer_unaligned (acfg
, target
);
2203 mono_img_writer_emit_pointer (MonoImageWriter
*acfg
, const char *target
)
2205 #ifdef USE_BIN_WRITER
2206 if (acfg
->use_bin_writer
)
2207 bin_writer_emit_pointer (acfg
, target
);
2209 asm_writer_emit_pointer (acfg
, target
);
2211 asm_writer_emit_pointer (acfg
, target
);
2216 mono_img_writer_emit_int16 (MonoImageWriter
*acfg
, int value
)
2218 #ifdef USE_BIN_WRITER
2219 if (acfg
->use_bin_writer
)
2220 bin_writer_emit_int16 (acfg
, value
);
2222 asm_writer_emit_int16 (acfg
, value
);
2224 asm_writer_emit_int16 (acfg
, value
);
2229 mono_img_writer_emit_int32 (MonoImageWriter
*acfg
, int value
)
2231 #ifdef USE_BIN_WRITER
2232 if (acfg
->use_bin_writer
)
2233 bin_writer_emit_int32 (acfg
, value
);
2235 asm_writer_emit_int32 (acfg
, value
);
2237 asm_writer_emit_int32 (acfg
, value
);
2242 mono_img_writer_emit_symbol_diff (MonoImageWriter
*acfg
, const char *end
, const char* start
, int offset
)
2244 #ifdef USE_BIN_WRITER
2245 if (acfg
->use_bin_writer
)
2246 bin_writer_emit_symbol_diff (acfg
, end
, start
, offset
);
2248 asm_writer_emit_symbol_diff (acfg
, end
, start
, offset
);
2250 asm_writer_emit_symbol_diff (acfg
, end
, start
, offset
);
2255 mono_img_writer_emit_zero_bytes (MonoImageWriter
*acfg
, int num
)
2257 #ifdef USE_BIN_WRITER
2258 if (acfg
->use_bin_writer
)
2259 bin_writer_emit_zero_bytes (acfg
, num
);
2261 asm_writer_emit_zero_bytes (acfg
, num
);
2263 asm_writer_emit_zero_bytes (acfg
, num
);
2268 mono_img_writer_emit_writeout (MonoImageWriter
*acfg
)
2270 #ifdef USE_BIN_WRITER
2271 if (acfg
->use_bin_writer
)
2272 return bin_writer_emit_writeout (acfg
);
2274 return asm_writer_emit_writeout (acfg
);
2276 return asm_writer_emit_writeout (acfg
);
2281 mono_img_writer_emit_byte (MonoImageWriter
*acfg
, guint8 val
)
2283 mono_img_writer_emit_bytes (acfg
, &val
, 1);
2287 * Emit a relocation entry of type RELOC_TYPE against symbol SYMBOL at the current PC.
2288 * Do not advance PC.
2291 mono_img_writer_emit_reloc (MonoImageWriter
*acfg
, int reloc_type
, const char *symbol
, int addend
)
2293 /* This is only supported by the bin writer */
2294 #ifdef USE_BIN_WRITER
2295 if (acfg
->use_bin_writer
)
2296 bin_writer_emit_reloc (acfg
, reloc_type
, symbol
, addend
);
2298 g_assert_not_reached ();
2300 g_assert_not_reached ();
2305 * mono_img_writer_emit_unset_mode:
2307 * Flush buffered data so it is safe to write to the output file from outside this
2308 * module. This is a nop for the binary writer.
2311 mono_img_writer_emit_unset_mode (MonoImageWriter
*acfg
)
2313 if (!acfg
->use_bin_writer
)
2314 asm_writer_emit_unset_mode (acfg
);
2318 * mono_img_writer_get_output:
2320 * Return the output buffer of a binary writer emitting to memory. The returned memory
2321 * is from malloc, and it is owned by the caller.
2324 mono_img_writer_get_output (MonoImageWriter
*acfg
, guint32
*size
)
2326 #ifdef USE_BIN_WRITER
2329 g_assert (acfg
->use_bin_writer
);
2331 buf
= acfg
->out_buf
;
2332 *size
= acfg
->out_buf_size
;
2333 acfg
->out_buf
= NULL
;
2336 g_assert_not_reached ();
2342 * Return whenever the binary writer is supported on this platform.
2345 mono_bin_writer_supported (void)
2347 #ifdef USE_BIN_WRITER
2355 * mono_img_writer_create:
2357 * Create an image writer writing to FP. If USE_BIN_WRITER is TRUE, FP can be NULL,
2358 * in this case the image writer will write to a memory buffer obtainable by calling
2359 * mono_img_writer_get_output ().
2362 mono_img_writer_create (FILE *fp
, gboolean use_bin_writer
)
2364 MonoImageWriter
*w
= g_new0 (MonoImageWriter
, 1);
2366 #ifndef USE_BIN_WRITER
2367 g_assert (!use_bin_writer
);
2370 if (!use_bin_writer
)
2374 w
->use_bin_writer
= use_bin_writer
;
2375 w
->mempool
= mono_mempool_new ();
2381 mono_img_writer_destroy (MonoImageWriter
*w
)
2383 // FIXME: Free all the stuff
2384 mono_mempool_destroy (w
->mempool
);
2389 mono_img_writer_subsections_supported (MonoImageWriter
*acfg
)
2391 #ifdef TARGET_ASM_APPLE
2392 return acfg
->use_bin_writer
;
2399 mono_img_writer_get_fp (MonoImageWriter
*acfg
)
2405 mono_img_writer_get_temp_label_prefix (MonoImageWriter
*acfg
)
2407 return AS_TEMP_LABEL_PREFIX
;