tags:

views:

231

answers:

2

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
+1  A: 

there are few lines of assembly language. other are definitions (all that start with dot (.)) most of them are memory allocations, other you can easily find in gcc docs.

LFB2:
    pushq   %rbp
LCFI0:
    movq    %rsp, %rbp
LCFI1:
    leave
    ret

this is entering/exiting stack frame.

Andrey
+4  A: 

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.

caf
Odd that there are C++ features in there. I produced the output with gcc.