Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I've been playing around with assembly and object files in general on Mac OS Ⅹ and was wondering if somebody could provide some edification. Specifically, I'm wondering what the extra code GCC generates when compiling the C file in the following example does.

I have a toy C program so I can comprehend the assembly output.

int main() {
int a = 5;
int b = 5;
int c = a + b; }

Running this through gcc -S creates the following assembly:

 .text
.globl _main
_main:
LFB2:
    pushq   %rbp
LCFI0:
    movq    %rsp, %rbp
LCFI1:
    movl    $5, -4(%rbp)
    movl    $5, -8(%rbp)
    movl    -8(%rbp), %eax
    addl    -4(%rbp), %eax
    movl    %eax, -12(%rbp)
    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

The LCFI1 section seems to contain the actual logic for the program, but I'm not sure what the misc. other stuff is for... also, is there any scheme these labels are following?

I'm sorry this is such a vague question. I'd appreciate anything, including being pointed to a resource where I can find out more about this. Thanks!

share|improve this question
1  
This is just a generic GCC question. It has nothing to do with Macintosh. – S.Lott Jan 11 '11 at 0:28
My mistake. I did try to tag it as “GCC”, but I don't have enough points it seems. – veryfoolish Jan 11 '11 at 1:45
1  
Check stackoverflow.com/questions/137523/… – Yar Jan 11 '11 at 5:35
3  
What's with the weird "X" in the question title? – eykanal Apr 18 '12 at 13:31
2  
@eykanal it's Unicode Character 'ROMAN NUMERAL TEN' (U+2169). Someone's being needlessly pedantic, I would say, but then this is a site for programmers :) – AakashM Apr 18 '12 at 13:45

3 Answers

up vote 3 down vote accepted

With (Fink) gcc 4.2.1 on Mac OS X, gcc -S gives instead the following more straightforward code:

        .text
.globl _main
_main:
    pushl   %ebp
    movl    %esp, %ebp
    subl    $24, %esp
    movl    $5, -12(%ebp)
    movl    $5, -16(%ebp)
    movl    -16(%ebp), %eax
    addl    -12(%ebp), %eax
    movl    %eax, -20(%ebp)
    leave
    ret
    .subsections_via_symbols

So, I would not pay too much attention to the strange lines you get.

However, using the otx disassembler yields a bit more code, which may be the part responsible for returning the (0) error code to the system?

start:  
    +0      00001f34  6a00                                    pushl           $0x00
    +2      00001f36  89e5                                    movl            %esp,%ebp
    +4      00001f38  83e4f0                                  andl            $0xf0,%esp
    +7      00001f3b  83ec10                                  subl            $0x10,%esp
   +10  00001f3e  8b5d04                                  movl            0x04(%ebp),%ebx
   +13  00001f41  891c24                                  movl            %ebx,(%esp)
   +16  00001f44  8d4d08                                  leal            0x08(%ebp),%ecx
   +19  00001f47  894c2404                                movl            %ecx,0x04(%esp)
   +23  00001f4b  83c301                                  addl            $0x01,%ebx
   +26  00001f4e  c1e302                                  shll            $0x02,%ebx
   +29  00001f51  01cb                                    addl            %ecx,%ebx
   +31  00001f53  895c2408                                movl            %ebx,0x08(%esp)

   +35  00001f57  8b03                                    movl            (%ebx),%eax
   +37  00001f59  83c304                                  addl            $0x04,%ebx
   +40  00001f5c  85c0                                    testl           %eax,%eax
   +42  00001f5e  75f7                                    jne             0x100001f57

   +44  00001f60  895c240c                                movl            %ebx,0x0c(%esp)
   +48  00001f64  e809000000                      calll           _main
   +53  00001f69  890424                                  movl            %eax,(%esp)
   +56  00001f6c  e821000000                      calll           0x00001f92                                _exit
   +61  00001f71  f4                                      hlt

_main:  

    +0      00001f72  55                                      pushl           %ebp
    +1      00001f73  89e5                                    movl            %esp,%ebp
    +3      00001f75  83ec18                                  subl            $0x18,%esp
    +6      00001f78  c745f405000000                  movl            $0x00000005,0xf4(%ebp)
   +13  00001f7f  c745f005000000                  movl            $0x00000005,0xf0(%ebp)
   +20  00001f86  8b45f0                                  movl            0xf0(%ebp),%eax
   +23  00001f89  0345f4                                  addl            0xf4(%ebp),%eax
   +26  00001f8c  8945ec                                  movl            %eax,0xec(%ebp)
   +29  00001f8f  c9                                      leave
   +30  00001f90  c3                                      ret
share|improve this answer

It's been years since I wrote assembler, but what you could be seeing is the result of a disassembly pass trying to make sense of a buffer of random bytes.

"Back in the day", when I started in assembler, and was a hackin' fool, we'd disassemble everything we could just to see how things worked. If the disassembler got off track it would spew some pretty random code as it tried to make sense of things, which would often be long lists of data and byte records like you're seeing.

Also, +1 for you for taking the initiative to learn assembler. It is an awesome level to program in; Treacherous, but awesome.

share|improve this answer

Try to compile the c-program with:

gcc -O2 -Wa,-adhls -fno-asynchronous-unwind-tables -S program.c

That should give you pure assembly code without any extra labels.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.