From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 9503 invoked by alias); 18 Feb 2014 15:54:25 -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 9489 invoked by uid 89); 18 Feb 2014 15:54:25 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.7 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-qg0-f45.google.com Received: from mail-qg0-f45.google.com (HELO mail-qg0-f45.google.com) (209.85.192.45) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Tue, 18 Feb 2014 15:53:54 +0000 Received: by mail-qg0-f45.google.com with SMTP id j5so7187677qga.4 for ; Tue, 18 Feb 2014 07:53:52 -0800 (PST) MIME-Version: 1.0 X-Received: by 10.224.28.197 with SMTP id n5mr44064204qac.43.1392738832353; Tue, 18 Feb 2014 07:53:52 -0800 (PST) Received: by 10.140.28.137 with HTTP; Tue, 18 Feb 2014 07:53:52 -0800 (PST) Date: Tue, 18 Feb 2014 15:54:00 -0000 Message-ID: Subject: Assembling functions from gas api From: Philip Herron To: binutils@sourceware.org Content-Type: text/plain; charset=ISO-8859-1 X-IsSubscribed: yes X-SW-Source: 2014-02/txt/msg00105.txt.bz2 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