Pasted below is unoptimized GCC assembly output for "int main(){}". I'm relatively good with x86 assembly, but some of this is unfamiliar. Could someone please do a line-by-line walk-through of what's going on here?
Thanks!
.text
.globl _main
_main:
LFB2:
pushq %rbp
LCFI0:
movq %rsp, %rbp
LCFI1:
leave
ret
LFE2:
.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support
EH_frame1:
.set L$set$0,LECIE1-LSCIE1
.long L$set$0
LSCIE1:
.long 0x0
.byte 0x1
.ascii "zR\0"
.byte 0x1
.byte 0x78
.byte 0x10
.byte 0x1
.byte 0x10
.byte 0xc
.byte 0x7
.byte 0x8
.byte 0x90
.byte 0x1
.align 3
LECIE1:
.globl _main.eh
_main.eh:
LSFDE1:
.set L$set$1,LEFDE1-LASFDE1
.long L$set$1
LASFDE1:
.long LASFDE1-EH_frame1
.quad LFB2-.
.set L$set$2,LFE2-LFB2
.quad L$set$2
.byte 0x0
.byte 0x4
.set L$set$3,LCFI0-LFB2
.long L$set$3
.byte 0xe
.byte 0x10
.byte 0x86
.byte 0x2
.byte 0x4
.set L$set$4,LCFI1-LCFI0
.long L$set$4
.byte 0xd
.byte 0x6
.align 3
LEFDE1:
.subsections_via_symbols
Luckily, gcc does not output binary machine code directly. Instead, it internally writes assembler code, which then is translated by as into binary machine code (actually, gcc creates more intermediate structures). This internal assembler code can be outputted to a file, with some annotation to make it easier to read.
Master C and Embedded C Programming- Learn as you go The gcc provides a great feature to get all intermediate outputs from a source code while executing. To get the assembler output we can use the option '-S' for the gcc. This option shows the output after compiling, but before sending to the assembler.
The well-known GNU C/C++ Compiler (GCC), an optimizing 32-bit compiler at the heart of the GNU project, supports the x86 architecture quite well, and includes the ability to insert assembly code in C programs, in such a way that register allocation can be either specified or left to GCC.
The GNU Assembler, commonly known as gas or as, is the assembler developed by the GNU Project. It is the default back-end of GCC. It is used to assemble the GNU operating system and the Linux kernel, and various other software.
Tell the linker to put this into the executable's .text
section:
.text
Export main
as a external symbol:
.globl _main
Define the main
function itself:
_main:
LFB2:
Save the previous frame pointer:
pushq %rbp
LCFI0:
Set up a new frame pointer:
movq %rsp, %rbp
LCFI1:
Restore the old frame pointer and return to caller:
leave
ret
The following directives are setting up an .eh_frame
section, containing information required by the C++ runtime for exception handling.
LFE2:
.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support
This is the Common Information Entry table:
EH_frame1:
It starts with a length, calculated from the difference of the LSCIE1
and LECIE1
labels:
.set L$set$0,LECIE1-LSCIE1
.long L$set$0
(The .long
, .byte
, .ascii
and .quad
cause a value of the appropriate type to be directly emitted by the assembler). Then follows the CIE table itself:
LSCIE1:
.long 0x0
.byte 0x1
.ascii "zR\0"
.byte 0x1
.byte 0x78
.byte 0x10
.byte 0x1
.byte 0x10
.byte 0xc
.byte 0x7
.byte 0x8
.byte 0x90
.byte 0x1
.align 3
LECIE1:
Another external symbol, this one for the main
function's Frame Description Entry (still part of the exception handling information):
.globl _main.eh
_main.eh:
Again, the FDE starts with a length:
LSFDE1:
.set L$set$1,LEFDE1-LASFDE1
.long L$set$1
..and continues with the rest of the FDE table.
LASFDE1:
.long LASFDE1-EH_frame1
.quad LFB2-.
.set L$set$2,LFE2-LFB2
.quad L$set$2
.byte 0x0
.byte 0x4
.set L$set$3,LCFI0-LFB2
.long L$set$3
.byte 0xe
.byte 0x10
.byte 0x86
.byte 0x2
.byte 0x4
.set L$set$4,LCFI1-LCFI0
.long L$set$4
.byte 0xd
.byte 0x6
.align 3
LEFDE1:
.subsections_via_symbols
Those exception handling tables are mostly uninteresting, but if you want to know then information on the format is available here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With