본문 바로가기

Language/c/c++

GCC 어셈블리 코드와 소스 코드를 한번에 보는 방법


#include <stdio.h>

int main()
{
    int i = 0;

    for (i = 0; i < 5; i ++ )
    {
        printf("Hello World\n");
    }

    return 0;
}

위와 같은 소스 코드를 컴파일하여 어셈블리 코드와 소스 코드를 보는 방법은 아래와 같다.

gcc -c -g hello.cpp
와 같이 컴파일하여 오브젝트 코드를 얻는다.

objdump -dS hello.o > hello.txt
와 하면 아래와 같이 어셈블리 코드와 소스 코드를 한번에 확인할 수 있다.

test.o:     file format elf32-i386

Disassembly of section .init:

08048348 <_init>:
 8048348:    55                       push   %ebp
 8048349:    89 e5                    mov    %esp,%ebp
 804834b:    53                       push   %ebx
 804834c:    83 ec 04                 sub    $0x4,%esp
 804834f:    e8 00 00 00 00           call   8048354 <_init+0xc>
 8048354:    5b                       pop    %ebx
 8048355:    81 c3 88 13 00 00        add    $0x1388,%ebx
 804835b:    8b 93 fc ff ff ff        mov    -0x4(%ebx),%edx
 8048361:    85 d2                    test   %edx,%edx
 8048363:    74 05                    je     804836a <_init+0x22>
 8048365:    e8 1e 00 00 00           call   8048388 <__gmon_start__@plt>
 804836a:    e8 c1 00 00 00           call   8048430 <frame_dummy>
 804836f:    e8 9c 01 00 00           call   8048510 <__do_global_ctors_aux>
 8048374:    58                       pop    %eax
 8048375:    5b                       pop    %ebx
 8048376:    c9                       leave 
 8048377:    c3                       ret   
Disassembly of section .plt:

08048378 <__gmon_start__@plt-0x10>:
 8048378:    ff 35 e0 96 04 08        pushl  0x80496e0
 804837e:    ff 25 e4 96 04 08        jmp    *0x80496e4
 8048384:    00 00                    add    %al,(%eax)
    ...

08048388 <__gmon_start__@plt>:
 8048388:    ff 25 e8 96 04 08        jmp    *0x80496e8
 804838e:    68 00 00 00 00           push   $0x0
 8048393:    e9 e0 ff ff ff           jmp    8048378 <_init+0x30>

08048398 <__libc_start_main@plt>:
 8048398:    ff 25 ec 96 04 08        jmp    *0x80496ec
 804839e:    68 08 00 00 00           push   $0x8
 80483a3:    e9 d0 ff ff ff           jmp    8048378 <_init+0x30>

080483a8 <puts@plt>:
 80483a8:    ff 25 f0 96 04 08        jmp    *0x80496f0
 80483ae:    68 10 00 00 00           push   $0x10
 80483b3:    e9 c0 ff ff ff           jmp    8048378 <_init+0x30>

080483b8 <__gxx_personality_v0@plt>:
 80483b8:    ff 25 f4 96 04 08        jmp    *0x80496f4
 80483be:    68 18 00 00 00           push   $0x18
 80483c3:    e9 b0 ff ff ff           jmp    8048378 <_init+0x30>
Disassembly of section .text:

080483d0 <_start>:
 80483d0:    31 ed                    xor    %ebp,%ebp
 80483d2:    5e                       pop    %esi
 80483d3:    89 e1                    mov    %esp,%ecx
 80483d5:    83 e4 f0                 and    $0xfffffff0,%esp
 80483d8:    50                       push   %eax
 80483d9:    54                       push   %esp
 80483da:    52                       push   %edx
 80483db:    68 a0 84 04 08           push   $0x80484a0
 80483e0:    68 b0 84 04 08           push   $0x80484b0
 80483e5:    51                       push   %ecx
 80483e6:    56                       push   %esi
 80483e7:    68 54 84 04 08           push   $0x8048454
 80483ec:    e8 a7 ff ff ff           call   8048398 <__libc_start_main@plt>
 80483f1:    f4                       hlt   
 80483f2:    90                       nop   
 80483f3:    90                       nop   
 80483f4:    90                       nop   
 80483f5:    90                       nop   
 80483f6:    90                       nop   
 80483f7:    90                       nop   
 80483f8:    90                       nop   
 80483f9:    90                       nop   
 80483fa:    90                       nop   
 80483fb:    90                       nop   
 80483fc:    90                       nop   
 80483fd:    90                       nop   
 80483fe:    90                       nop   
 80483ff:    90                       nop   

08048400 <__do_global_dtors_aux>:
 8048400:    55                       push   %ebp
 8048401:    89 e5                    mov    %esp,%ebp
 8048403:    83 ec 08                 sub    $0x8,%esp
 8048406:    80 3d 04 97 04 08 00     cmpb   $0x0,0x8049704
 804840d:    74 0c                    je     804841b <__do_global_dtors_aux+0x1b>
 804840f:    eb 1c                    jmp    804842d <__do_global_dtors_aux+0x2d>
 8048411:    83 c0 04                 add    $0x4,%eax
 8048414:    a3 00 97 04 08           mov    %eax,0x8049700
 8048419:    ff d2                    call   *%edx
 804841b:    a1 00 97 04 08           mov    0x8049700,%eax
 8048420:    8b 10                    mov    (%eax),%edx
 8048422:    85 d2                    test   %edx,%edx
 8048424:    75 eb                    jne    8048411 <__do_global_dtors_aux+0x11>
 8048426:    c6 05 04 97 04 08 01     movb   $0x1,0x8049704
 804842d:    c9                       leave 
 804842e:    c3                       ret   
 804842f:    90                       nop   

08048430 <frame_dummy>:
 8048430:    55                       push   %ebp
 8048431:    89 e5                    mov    %esp,%ebp
 8048433:    83 ec 08                 sub    $0x8,%esp
 8048436:    a1 ec 95 04 08           mov    0x80495ec,%eax
 804843b:    85 c0                    test   %eax,%eax
 804843d:    74 12                    je     8048451 <frame_dummy+0x21>
 804843f:    b8 00 00 00 00           mov    $0x0,%eax
 8048444:    85 c0                    test   %eax,%eax
 8048446:    74 09                    je     8048451 <frame_dummy+0x21>
 8048448:    c7 04 24 ec 95 04 08     movl   $0x80495ec,(%esp)
 804844f:    ff d0                    call   *%eax
 8048451:    c9                       leave 
 8048452:    c3                       ret   
 8048453:    90                       nop   

08048454 <main>:
#include <stdio.h>

int main()
 8048454:    8d 4c 24 04              lea    0x4(%esp),%ecx
 8048458:    83 e4 f0                 and    $0xfffffff0,%esp
 804845b:    ff 71 fc                 pushl  -0x4(%ecx)
 804845e:    55                       push   %ebp
 804845f:    89 e5                    mov    %esp,%ebp
 8048461:    51                       push   %ecx
 8048462:    83 ec 14                 sub    $0x14,%esp
{
    int i = 0;
 8048465:    c7 45 f8 00 00 00 00     movl   $0x0,-0x8(%ebp)

    for (i = 0; i < 5; i ++ )
 804846c:    c7 45 f8 00 00 00 00     movl   $0x0,-0x8(%ebp)
 8048473:    eb 10                    jmp    8048485 <main+0x31>
    {
        printf("Hello World\n");
 8048475:    c7 04 24 60 85 04 08     movl   $0x8048560,(%esp)
 804847c:    e8 27 ff ff ff           call   80483a8 <puts@plt>

int main()
{
    int i = 0;

    for (i = 0; i < 5; i ++ )
 8048481:    83 45 f8 01              addl   $0x1,-0x8(%ebp)
 8048485:    83 7d f8 04              cmpl   $0x4,-0x8(%ebp)
 8048489:    7e ea                    jle    8048475 <main+0x21>
    {
        printf("Hello World\n");
    }

    return 0;
 804848b:    b8 00 00 00 00           mov    $0x0,%eax
}
 8048490:    83 c4 14                 add    $0x14,%esp
 8048493:    59                       pop    %ecx
 8048494:    5d                       pop    %ebp
 8048495:    8d 61 fc                 lea    -0x4(%ecx),%esp
 8048498:    c3                       ret   
 8048499:    90                       nop   
 804849a:    90                       nop   
 804849b:    90                       nop   
 804849c:    90                       nop   
 804849d:    90                       nop   
 804849e:    90                       nop   
 804849f:    90                       nop   

080484a0 <__libc_csu_fini>:
 80484a0:    55                       push   %ebp
 80484a1:    89 e5                    mov    %esp,%ebp
 80484a3:    5d                       pop    %ebp
 80484a4:    c3                       ret   
 80484a5:    8d 74 26 00              lea    0x0(%esi),%esi
 80484a9:    8d bc 27 00 00 00 00     lea    0x0(%edi),%edi

080484b0 <__libc_csu_init>:
 80484b0:    55                       push   %ebp
 80484b1:    89 e5                    mov    %esp,%ebp
 80484b3:    57                       push   %edi
 80484b4:    56                       push   %esi
 80484b5:    53                       push   %ebx
 80484b6:    e8 4f 00 00 00           call   804850a <__i686.get_pc_thunk.bx>
 80484bb:    81 c3 21 12 00 00        add    $0x1221,%ebx
 80484c1:    83 ec 0c                 sub    $0xc,%esp
 80484c4:    e8 7f fe ff ff           call   8048348 <_init>
 80484c9:    8d bb 00 ff ff ff        lea    -0x100(%ebx),%edi
 80484cf:    8d 83 00 ff ff ff        lea    -0x100(%ebx),%eax
 80484d5:    29 c7                    sub    %eax,%edi
 80484d7:    c1 ff 02                 sar    $0x2,%edi
 80484da:    85 ff                    test   %edi,%edi
 80484dc:    74 24                    je     8048502 <__libc_csu_init+0x52>
 80484de:    31 f6                    xor    %esi,%esi
 80484e0:    8b 45 10                 mov    0x10(%ebp),%eax
 80484e3:    89 44 24 08              mov    %eax,0x8(%esp)
 80484e7:    8b 45 0c                 mov    0xc(%ebp),%eax
 80484ea:    89 44 24 04              mov    %eax,0x4(%esp)
 80484ee:    8b 45 08                 mov    0x8(%ebp),%eax
 80484f1:    89 04 24                 mov    %eax,(%esp)
 80484f4:    ff 94 b3 00 ff ff ff     call   *-0x100(%ebx,%esi,4)
 80484fb:    83 c6 01                 add    $0x1,%esi
 80484fe:    39 f7                    cmp    %esi,%edi
 8048500:    75 de                    jne    80484e0 <__libc_csu_init+0x30>
 8048502:    83 c4 0c                 add    $0xc,%esp
 8048505:    5b                       pop    %ebx
 8048506:    5e                       pop    %esi
 8048507:    5f                       pop    %edi
 8048508:    5d                       pop    %ebp
 8048509:    c3                       ret   

0804850a <__i686.get_pc_thunk.bx>:
 804850a:    8b 1c 24                 mov    (%esp),%ebx
 804850d:    c3                       ret   
 804850e:    90                       nop   
 804850f:    90                       nop   

08048510 <__do_global_ctors_aux>:
 8048510:    55                       push   %ebp
 8048511:    89 e5                    mov    %esp,%ebp
 8048513:    53                       push   %ebx
 8048514:    83 ec 04                 sub    $0x4,%esp
 8048517:    a1 dc 95 04 08           mov    0x80495dc,%eax
 804851c:    83 f8 ff                 cmp    $0xffffffff,%eax
 804851f:    74 12                    je     8048533 <__do_global_ctors_aux+0x23>
 8048521:    31 db                    xor    %ebx,%ebx
 8048523:    ff d0                    call   *%eax
 8048525:    8b 83 d8 95 04 08        mov    0x80495d8(%ebx),%eax
 804852b:    83 eb 04                 sub    $0x4,%ebx
 804852e:    83 f8 ff                 cmp    $0xffffffff,%eax
 8048531:    75 f0                    jne    8048523 <__do_global_ctors_aux+0x13>
 8048533:    83 c4 04                 add    $0x4,%esp
 8048536:    5b                       pop    %ebx
 8048537:    5d                       pop    %ebp
 8048538:    c3                       ret   
 8048539:    90                       nop   
 804853a:    90                       nop   
 804853b:    90                       nop   
Disassembly of section .fini:

0804853c <_fini>:
 804853c:    55                       push   %ebp
 804853d:    89 e5                    mov    %esp,%ebp
 804853f:    53                       push   %ebx
 8048540:    83 ec 04                 sub    $0x4,%esp
 8048543:    e8 00 00 00 00           call   8048548 <_fini+0xc>
 8048548:    5b                       pop    %ebx
 8048549:    81 c3 94 11 00 00        add    $0x1194,%ebx
 804854f:    e8 ac fe ff ff           call   8048400 <__do_global_dtors_aux>
 8048554:    59                       pop    %ecx
 8048555:    5b                       pop    %ebx
 8048556:    c9                       leave 
 8048557:    c3                       ret   



Reference
  • http://24alpha.wordpress.com/2007/12/18/how-to-get-gcc-to-interleave-assembly-output-with-original-source-code/