Lab 04: Compiled C Lab

In this lab we were tasked to write a program and compile it with different compiler options in GCC, also check the difference withing the Disassembly part of the code in the objdump output.

Both the code I wrote and the string to be printed can be found in the <main> section of the disassembly part of the objdump output.

/*Added the compiler option -static*/

Command:
gcc -g -static -O0 -fno-builtin hello.c
objdump -f -s -d –source | less

000000000040096e <main>:
#include <stdio.h>

int main(){
40096e:       55                      push   %rbp
40096f:       48 89 e5                mov    %rsp,%rbp
printf(“Hello World\n”);
400972:       48 8d 3d cb 6b 08 00    lea    0x86bcb(%rip),%rdi        # 487544 <_IO_stdin_used+0x4>
400979:       b8 00 00 00 00          mov    $0x0,%eax
40097e:       e8 5d 6a 00 00          callq  4073e0 <_IO_printf>
400983:       b8 00 00 00 00          mov    $0x0,%eax
}
400988:       5d                      pop    %rbp
400989:       c3                      retq
40098a:       66 0f 1f 44 00 00       nopw   0x0(%rax,%rax,1)

Differences:

-The size of each section increases.

-Some of the section headers like .gnu.hash, .dynsym, and .dynstr are no longer part of the objdump.

-Each line number ID is now continuous.

-Some of the <main> output changed.

–before:
00000000000006c0 <main>:
#include <stdio.h>

int main(){
6c0:   55                      push   %rbp
6c1:   48 89 e5                mov    %rsp,%rbp
printf(“Hello World\n”);
6c4:   48 8d 3d 99 00 00 00    lea    0x99(%rip),%rdi        # 764 <_IO_stdin_used+0x4>
6cb:   b8 00 00 00 00          mov    $0x0,%eax
6d0:   e8 8b fe ff ff          callq  560 <printf@plt>
6d5:   b8 00 00 00 00          mov    $0x0,%eax
}
6da:   5d                      pop    %rbp
6db:   c3                      retq
6dc:   0f 1f 40 00             nopl   0x0(%rax)

/*Removed the compileroption -fno-builtin*/

Commands:
gcc -g -static -O0 hello.c
objdump -f -s -d –source | less

The differences in the function call are in these lines:

-before
400972:       48 8d 3d cb 6b 08 00    lea    0x86bcb(%rip),%rdi        # 487544 <_IO_stdin_used+0x4>
400979:       b8 00 00 00 00          mov    $0x0,%eax
40097e:       e8 5d 6a 00 00          callq  4073e0 <_IO_printf>
400983:       b8 00 00 00 00          mov    $0x0,%eax

400988:       5d                      pop    %rbp
400989:       c3                      retq
40098a:       66 0f 1f 44 00 00       nopw   0x0(%rax,%rax,1)

-after
400972:       48 8d 3d eb 6c 08 00    lea    0x86ceb(%rip),%rdi        # 487664 <_IO_stdin_used+0x4>
400979:       e8 22 71 00 00          callq  407aa0 <_IO_puts>
40097e:       b8 00 00 00 00          mov    $0x0,%eax

400983:       5d                      pop    %rbp
400984:       c3                      retq
400985:       66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)
40098c:       00 00 00
40098f:       90                      nop

/*Removed the compiler option -g*/

Commands:
gcc -static -O0 hello.c
objdump -f -s -d –source | less

-Some sections in the objdump output have increased

-The section headers have not changed at all.

-Function call changes:
000000000040096e <main>:
#include <stdio.h>

int main(){
40096e:       55                      push   %rbp
40096f:       48 89 e5                mov    %rsp,%rbp
printf(“Hello World\n”);
400972:       48 8d 3d eb 6c 08 00    lea    0x86ceb(%rip),%rdi        # 487664 <_IO_stdin_used+0x4>
400979:       e8 22 71 00 00          callq  407aa0 <_IO_puts>
40097e:       b8 00 00 00 00          mov    $0x0,%eax
}
400983:       5d                      pop    %rbp
400984:       c3                      retq
400985:       66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)
40098c:       00 00 00
40098f:       90                      nop

000000000040096e <main>:
40096e:       55                      push   %rbp
40096f:       48 89 e5                mov    %rsp,%rbp
400972:       48 8d 3d eb 6c 08 00    lea    0x86ceb(%rip),%rdi        #
487664 <_IO_stdin_used+0x4>
400979:       e8 22 71 00 00          callq  407aa0 <_IO_puts>
40097e:       b8 00 00 00 00          mov    $0x0,%eax
400983:       5d                      pop    %rbp
400984:       c3                      retq
400985:       66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)
40098c:       00 00 00
40098f:       90                      nop

The disassembly main does not give a detailed description of the code and which lines are related to which part of the code.

/*Added arguments into the code of hello.c so it has 10 arguments instead of 1.*/

Commands:
gcc -static -O0 hello.c
objdump -f -s -d –source | less

000000000040096e <main>:
40096e:       55                      push   %rbp
40096f:       48 89 e5                mov    %rsp,%rbp
400972:       6a 09                   pushq  $0x9
400974:       6a 08                   pushq  $0x8
400976:       6a 07                   pushq  $0x7
400978:       6a 06                   pushq  $0x6
40097a:       41 b9 05 00 00 00       mov    $0x5,%r9d
400980:       41 b8 04 00 00 00       mov    $0x4,%r8d
400986:       b9 03 00 00 00          mov    $0x3,%ecx
40098b:       ba 02 00 00 00          mov    $0x2,%edx
400990:       be 01 00 00 00          mov    $0x1,%esi
400995:       48 8d 3d cc 6b 08 00    lea    0x86bcc(%rip),%rdi        # 487568 <_IO_stdin_used+0x8>
40099c:       b8 00 00 00 00          mov    $0x0,%eax
4009a1:       e8 6a 6a 00 00          callq  407410 <_IO_printf>
4009a6:       48 83 c4 20             add    $0x20,%rsp
4009aa:       b8 00 00 00 00          mov    $0x0,%eax
4009af:       c9                      leaveq
4009b0:       c3                      retq
4009b1:       66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)
4009b8:       00 00 00
4009bb:       0f 1f 44 00 00          nopl   0x0(%rax,%rax,1)

All arguments are place in registers from 0x1 to 0x9 in reverse order.

/*Moved the printf() call to a separate function named output()*/

Commands:
gcc -static -O0 hello.c
objdump -f -s -d –source | less

00000000004009ad <main>:

int main(){
4009ad:       55                      push   %rbp
4009ae:       48 89 e5                mov    %rsp,%rbp
output();
4009b1:       b8 00 00 00 00          mov    $0x0,%eax
4009b6:       e8 b3 ff ff ff          callq  40096e <output>
4009bb:       b8 00 00 00 00          mov    $0x0,%eax
}
4009c0:       5d                      pop    %rbp
4009c1:       c3                      retq
4009c2:       66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)
4009c9:       00 00 00
4009cc:       0f 1f 40 00             nopl   0x0(%rax)

Now instead of givin a different value to an array of directories it only calls the memory address of the function output(), and outputs it.

/*Removed -O0 and added -O3 to the compiler option*/

Commands:
gcc -static -O3 hello.c
objdump -f -s -d –source | less

00000000004005b0 <main>:

void output(){
printf(“Hello World\n %d, %d, %d, %d, %d, %d, %d, %d, %d\n”, 1, 2, 3, 4, 5, 6, 7, 8, 9);
}

int main(){
4005b0:       48 83 ec 08             sub    $0x8,%rsp
output();
4005b4:       31 c0                   xor    %eax,%eax
4005b6:       e8 d5 03 00 00          callq  400990 <output>
}
4005bb:       31 c0                   xor    %eax,%eax
4005bd:       48 83 c4 08             add    $0x8,%rsp
4005c1:       c3                      retq
4005c2:       66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)
4005c9:       00 00 00
4005cc:       0f 1f 40 00             nopl   0x0(%rax)

There is not much difference in my opinion. Did not really find anything different in the compiled code.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s