This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: Assembling functions from gas api
- From: Richard Sandiford <rdsandiford at googlemail dot com>
- To: Philip Herron <redbrain at gcc dot gnu dot org>
- Cc: binutils at sourceware dot org
- Date: Wed, 19 Feb 2014 20:28:57 +0000
- Subject: Re: Assembling functions from gas api
- Authentication-results: sourceware.org; auth=none
- References: <CAEvRbepcPJ6Th=-rZTCj+V8i0CEEDOCt4GpJ6SwZq6=0dNL8iQ at mail dot gmail dot com>
Philip Herron <redbrain@gcc.gnu.org> writes:
> Hey all,
>
> I am trying to assemble this function:
>
> .code32
> .section .data
> format_string: .string "val = <%i>\n"
> .section .text
> .globl test
> test:
> push $5
> lea format_string, %edi
> push %edi
> call printf
> pop %eax
> pop %eax
> ret
>
> When i compile it with:
>
> $ gcc -m32 -c t2.c -o t2.o
> $ gcc -m32 -c t1.s -o t1.o
> $ gcc -m32 -o test t1.o t2.o
>
> The t2.c is simply calling test ().
>
> This works 100% and the t1.o file is:608 bytes in size. I've been
> following the gas api o assemble an input file and come up with this
> so far:
>
> int main (int argc, char **argv)
> {
> char * out_file_name = "./ninja";
> need_pass_2 = 0;
>
> bfd_init ();
> symbol_begin ();
> frag_init ();
> subsegs_begin ();
> read_begin ();
> input_scrub_begin ();
> expr_begin ();
>
> output_file_create (out_file_name);
> dot_symbol_init ();
>
> need_pass_2 = 0;
> listing = 1;
>
> text_section = subseg_new (TEXT_SECTION_NAME, 0);
> data_section = subseg_new (DATA_SECTION_NAME, 0);
> bss_section = subseg_new (BSS_SECTION_NAME, 0);
>
> flagword applicable = bfd_applicable_section_flags (stdoutput);
> bfd_set_section_flags (stdoutput, text_section,
> applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC
> | SEC_CODE | SEC_READONLY));
> bfd_set_section_flags (stdoutput, data_section,
> applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC
> | SEC_DATA));
> bfd_set_section_flags (stdoutput, bss_section, applicable & SEC_ALLOC);
> seg_info (bss_section)->bss = 1;
> subseg_new (BFD_ABS_SECTION_NAME, 0);
> subseg_new (BFD_UND_SECTION_NAME, 0);
> reg_section = subseg_new ("*GAS `reg' section*", 0);
> expr_section = subseg_new ("*GAS `expr' section*", 0);
> subseg_set (text_section, 0);
>
> md_begin ();
> elf_begin ();
> read_a_source_file ("./t1.s");
> cond_finish_check (-1);
>
> subsegs_finish ();
> write_object_file ();
> input_scrub_end ();
>
> return 0;
> }
>
> And the resulting file i called ninja has stuff in it but i don't
> think its correct. I realise more that i am playing with stuff i don't
> understand at all i was expecting this to output the exact same output
> as when i invoke the assembler but it doesn't. It outputs something
> very similar but not the same i think its missing the ELF stuff at the
> start of the output must be missing something. Maybe you have some
> help on how i can debug this.
You also need:
output_file_close (out_file_name);
or:
keep_it = 1;
...
close_output_file ();
This is normally run as an (x)atexit hook.
Thanks,
Richard