public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Philip Herron <redbrain@gcc.gnu.org>
To: binutils@sourceware.org
Subject: Assembling functions from gas api
Date: Tue, 18 Feb 2014 15:54:00 -0000	[thread overview]
Message-ID: <CAEvRbepcPJ6Th=-rZTCj+V8i0CEEDOCt4GpJ6SwZq6=0dNL8iQ@mail.gmail.com> (raw)

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

             reply	other threads:[~2014-02-18 15:54 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-18 15:54 Philip Herron [this message]
2014-02-19 20:29 ` Richard Sandiford

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAEvRbepcPJ6Th=-rZTCj+V8i0CEEDOCt4GpJ6SwZq6=0dNL8iQ@mail.gmail.com' \
    --to=redbrain@gcc.gnu.org \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).