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?
.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
Tell the linker to put this into the executable’s
.textsection:Export
mainas a external symbol:Define the
mainfunction itself:Save the previous frame pointer:
Set up a new frame pointer:
Restore the old frame pointer and return to caller:
The following directives are setting up an
.eh_framesection, containing information required by the C++ runtime for exception handling.This is the Common Information Entry table:
It starts with a length, calculated from the difference of the
LSCIE1andLECIE1labels:(The
.long,.byte,.asciiand.quadcause a value of the appropriate type to be directly emitted by the assembler). Then follows the CIE table itself:Another external symbol, this one for the
mainfunction’s Frame Description Entry (still part of the exception handling information):Again, the FDE starts with a length:
..and continues with the rest of the FDE table.
Those exception handling tables are mostly uninteresting, but if you want to know then information on the format is available here.