This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Assembling functions from gas api


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.

Thanks and sorry for the very vague question because i am messing with
the internals of something i don't understand that well quite yet.

--Phil


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]