Like Tree1Likes
  • 1 Post By Salem

How to assemble the output of gcc -S ?

This is a discussion on How to assemble the output of gcc -S ? within the Tech Board forums, part of the Community Boards category; I am reading a book on Assembly Language (Programming from the ground up)... and suddenly had the urge to learn ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    How to assemble the output of gcc -S ?

    I am reading a book on Assembly Language (Programming from the ground up)... and suddenly had the urge to learn by examining gcc's assembly output, because some of the computer architecture stuff boring to me now and seems not really necessary(I know the general picture pretty well) for getting the basics of asm.
    SO, I compiled
    Code:
    int main(void)
    {
            return 0;
    }
    to get
    Code:
            .file   "a.c"
            .text
    .globl main
            .type   main, @function
    main:
            pushl   %ebp
            movl    %esp, %ebp
            movl    $0, %eax
            popl    %ebp
            ret
            .size   main, .-main
            .ident  "GCC: (GNU) 4.6.1 20110908 (Red Hat 4.6.1-9)"
            .section        .note.GNU-stack,"",@progbits
    But this does not have a _start and the following fails to assemble and link it correctly.
    Code:
    as foo.s -o foo.o
    ld foo.o -o foo
    How should I modify the assembly and linking commands to correctly assemble the code without modification ?

    P.S.
    1. gcc's verbose option may show it .. but I am unable to decipher the gobbledygook.
    2. Linking libc in the linker step leads to a segfault at execution .
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    Assembler is machine dependent, so the techniques for assembling it are also machine dependent.

    The back end of gcc is based on the gnu assembler (xommonly known as gas). Google for "gas gnu".
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    GCC can assemble it by itself, using the the assembler mentioned by @grumpy, just by giving a (*.s) extension to the file.
    Devoted my life to programming...

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    ...
    It seems that 'gas' is same as 'as' (the only assembler in the binutils package is given as 'as' ).

    GCC can assemble it by itself
    Exactly what I'm looking for is the way it does so.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    Code:
    $ gcc -v foo.c
    Using built-in specs.
    Target: x86_64-linux-gnu
    Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
    Thread model: posix
    gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) 
    COLLECT_GCC_OPTIONS='-v' '-mtune=generic'
     /usr/lib/gcc/x86_64-linux-gnu/4.4.3/cc1 -quiet -v foo.c -D_FORTIFY_SOURCE=2 -quiet -dumpbase foo.c -mtune=generic -auxbase foo -version -fstack-protector -o /tmp/ccy9quou.s
    GNU C (Ubuntu 4.4.3-4ubuntu5) version 4.4.3 (x86_64-linux-gnu)
    	compiled by GNU C version 4.4.3, GMP version 4.3.2, MPFR version 2.4.2-p1.
    GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
    ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
    ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../x86_64-linux-gnu/include"
    ignoring nonexistent directory "/usr/include/x86_64-linux-gnu"
    #include "..." search starts here:
    #include <...> search starts here:
     /usr/local/include
     /usr/lib/gcc/x86_64-linux-gnu/4.4.3/include
     /usr/lib/gcc/x86_64-linux-gnu/4.4.3/include-fixed
     /usr/include
    End of search list.
    GNU C (Ubuntu 4.4.3-4ubuntu5) version 4.4.3 (x86_64-linux-gnu)
    	compiled by GNU C version 4.4.3, GMP version 4.3.2, MPFR version 2.4.2-p1.
    GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
    Compiler executable checksum: 2129e1a56226bd6e8f7af5e0a3ff467d
    COLLECT_GCC_OPTIONS='-v' '-mtune=generic'
     as -V -Qy -o /tmp/ccPaKnbN.o /tmp/ccy9quou.s
    GNU assembler version 2.20.1 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.20.1-system.20100303
    COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.4.3/:/usr/lib/gcc/x86_64-linux-gnu/4.4.3/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.4.3/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.4.3/:/usr/lib/gcc/x86_64-linux-gnu/
    LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.4.3/:/usr/lib/gcc/x86_64-linux-gnu/4.4.3/:/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../:/lib/:/usr/lib/:/usr/lib/x86_64-linux-gnu/
    COLLECT_GCC_OPTIONS='-v' '-mtune=generic'
     /usr/lib/gcc/x86_64-linux-gnu/4.4.3/collect2 --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=both -dynamic-linker /lib64/ld-linux-x86-64.so.2 -z relro /usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.4.3/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.4.3 -L/usr/lib/gcc/x86_64-linux-gnu/4.4.3 -L/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../.. -L/usr/lib/x86_64-linux-gnu /tmp/ccPaKnbN.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/4.4.3/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crtn.o
    $ gcc -S foo.c
    $ cat foo.s
    	.file	"foo.c"
    	.section	.rodata
    .LC0:
    	.string	"hello world"
    	.text
    .globl main
    	.type	main, @function
    main:
    .LFB0:
    	.cfi_startproc
    	pushq	%rbp
    	.cfi_def_cfa_offset 16
    	movq	%rsp, %rbp
    	.cfi_offset 6, -16
    	.cfi_def_cfa_register 6
    	subq	$16, %rsp
    	movl	%edi, -4(%rbp)
    	movq	%rsi, -16(%rbp)
    	movl	$.LC0, %edi
    	call	puts
    	movl	$0, %eax
    	leave
    	ret
    	.cfi_endproc
    .LFE0:
    	.size	main, .-main
    	.ident	"GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
    	.section	.note.GNU-stack,"",@progbits
    $ as -V -Qy -o foo.o foo.s
    GNU assembler version 2.20.1 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.20.1-system.20100303
    manasij7479 likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    ..Thanks..
    Can't believe I was lazy enough not to look, though.

    btw..I'm finally leaving the linking to gcc; that way, it works.
    If I do it with ld(possibly..er obviously... incorrectly), it still shows a segfault.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    IIRC GAS (Gnu Assembler) likes a "_start" entrypoint. If you assemble is with GCC it starts at "main"

    I prefer to work with NASM for this stuff, you can then output to ELF and link in with GCC and your C code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ overlapping output and adding extensions to output files
    By lordmorgul in forum Linux Programming
    Replies: 9
    Last Post: 05-11-2010, 08:26 AM
  2. How to edit output in struct and call for the output
    By andrewkho in forum C Programming
    Replies: 4
    Last Post: 03-16-2010, 10:28 PM
  3. terminal output not showing output properly
    By stanlvw in forum C Programming
    Replies: 13
    Last Post: 11-19-2007, 09:46 PM
  4. Computers what kind of idiots assemble them (i mean the shops)
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-28-2003, 07:43 AM
  5. Replies: 3
    Last Post: 02-19-2003, 07:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21