From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 17450 invoked by alias); 19 Feb 2014 20:29:04 -0000 Mailing-List: contact binutils-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: binutils-owner@sourceware.org Received: (qmail 17396 invoked by uid 89); 19 Feb 2014 20:29:03 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-we0-f177.google.com Received: from mail-we0-f177.google.com (HELO mail-we0-f177.google.com) (74.125.82.177) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Wed, 19 Feb 2014 20:29:02 +0000 Received: by mail-we0-f177.google.com with SMTP id t61so755816wes.22 for ; Wed, 19 Feb 2014 12:28:59 -0800 (PST) X-Received: by 10.180.19.130 with SMTP id f2mr3504469wie.6.1392841739034; Wed, 19 Feb 2014 12:28:59 -0800 (PST) Received: from localhost ([2.28.235.12]) by mx.google.com with ESMTPSA id de3sm3191983wjb.8.2014.02.19.12.28.57 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 19 Feb 2014 12:28:57 -0800 (PST) From: Richard Sandiford To: Philip Herron Mail-Followup-To: Philip Herron ,binutils@sourceware.org, rdsandiford@googlemail.com Cc: binutils@sourceware.org Subject: Re: Assembling functions from gas api In-Reply-To: (Philip Herron's message of "Tue, 18 Feb 2014 15:53:52 +0000") References: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Date: Wed, 19 Feb 2014 20:29:00 -0000 Message-ID: <87r46yc0dy.fsf@talisman.default> MIME-Version: 1.0 Content-Type: text/plain X-SW-Source: 2014-02/txt/msg00115.txt.bz2 Philip Herron 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