SPO Lab2 C Compiler

Written by Hunter Jansen on September 11, 2014

The second lab for my SPO600 course involves poking around in a c compiler’s output for a simple hello world program and trying to understand what I’m looking at.

The lab can be found here.

The Program

Like I said, it’s a super simple c program:

#include <stdio.h>

int main() {
    printf("Hello World!\n");
}

From there we compile with the gcc compiler using the following flags: -g, -O0, -fno-builtin. This produced an a.out file.

Following that, we’re to examine the a.out file using the objdump program using the following flags: -s, -f, -d, –source.

In the output, we’re then to identify the section containing the source, as well as the section containing the string to be printed.

###The source Obviously the source can be seen if you only use the –source flag on objdump. In performing that command, you’re provided with the output:

[hunter@li62-155 SPO600]$ objdump --source a.out 

a.out:     file format elf64-x86-64


Disassembly of section .init:

00000000004003e0 <_init>:
  4003e0:	48 83 ec 08          	sub    $0x8,%rsp
  4003e4:	48 8b 05 0d 0c 20 00 	mov    0x200c0d(%rip),%rax        # 600ff8 <_DYNAMIC+0x1d0>
  4003eb:	48 85 c0             	test   %rax,%rax
  4003ee:	74 05                	je     4003f5 <_init+0x15>
  4003f0:	e8 3b 00 00 00       	callq  400430 <__gmon_start__@plt>
  4003f5:	48 83 c4 08          	add    $0x8,%rsp
  4003f9:	c3                   	retq   

Disassembly of section .plt:

0000000000400400 <printf@plt-0x10>:
  400400:	ff 35 02 0c 20 00    	pushq  0x200c02(%rip)        # 601008 <_GLOBAL_OFFSET_TABLE_+0x8>
  400406:	ff 25 04 0c 20 00    	jmpq   *0x200c04(%rip)        # 601010 <_GLOBAL_OFFSET_TABLE_+0x10>
  40040c:	0f 1f 40 00          	nopl   0x0(%rax)

0000000000400410 <printf@plt>:
  400410:	ff 25 02 0c 20 00    	jmpq   *0x200c02(%rip)        # 601018 <_GLOBAL_OFFSET_TABLE_+0x18>
  400416:	68 00 00 00 00       	pushq  $0x0
  40041b:	e9 e0 ff ff ff       	jmpq   400400 <_init+0x20>

0000000000400420 <__libc_start_main@plt>:
  400420:	ff 25 fa 0b 20 00    	jmpq   *0x200bfa(%rip)        # 601020 <_GLOBAL_OFFSET_TABLE_+0x20>
  400426:	68 01 00 00 00       	pushq  $0x1
  40042b:	e9 d0 ff ff ff       	jmpq   400400 <_init+0x20>

0000000000400430 <__gmon_start__@plt>:
  400430:	ff 25 f2 0b 20 00    	jmpq   *0x200bf2(%rip)        # 601028 <_GLOBAL_OFFSET_TABLE_+0x28>
  400436:	68 02 00 00 00       	pushq  $0x2
  40043b:	e9 c0 ff ff ff       	jmpq   400400 <_init+0x20>

Disassembly of section .text:

0000000000400440 <_start>:
  400440:	31 ed                	xor    %ebp,%ebp
  400442:	49 89 d1             	mov    %rdx,%r9
  400445:	5e                   	pop    %rsi
  400446:	48 89 e2             	mov    %rsp,%rdx
  400449:	48 83 e4 f0          	and    $0xfffffffffffffff0,%rsp
  40044d:	50                   	push   %rax
  40044e:	54                   	push   %rsp
  40044f:	49 c7 c0 c0 05 40 00 	mov    $0x4005c0,%r8
  400456:	48 c7 c1 50 05 40 00 	mov    $0x400550,%rcx
  40045d:	48 c7 c7 30 05 40 00 	mov    $0x400530,%rdi
  400464:	e8 b7 ff ff ff       	callq  400420 <__libc_start_main@plt>
  400469:	f4                   	hlt    
  40046a:	66 90                	xchg   %ax,%ax
  40046c:	0f 1f 40 00          	nopl   0x0(%rax)

0000000000400470 <deregister_tm_clones>:
  400470:	b8 3f 10 60 00       	mov    $0x60103f,%eax
  400475:	55                   	push   %rbp
  400476:	48 2d 38 10 60 00    	sub    $0x601038,%rax
  40047c:	48 83 f8 0e          	cmp    $0xe,%rax
  400480:	48 89 e5             	mov    %rsp,%rbp
  400483:	77 02                	ja     400487 <deregister_tm_clones+0x17>
  400485:	5d                   	pop    %rbp
  400486:	c3                   	retq   
  400487:	b8 00 00 00 00       	mov    $0x0,%eax
  40048c:	48 85 c0             	test   %rax,%rax
  40048f:	74 f4                	je     400485 <deregister_tm_clones+0x15>
  400491:	5d                   	pop    %rbp
  400492:	bf 38 10 60 00       	mov    $0x601038,%edi
  400497:	ff e0                	jmpq   *%rax
  400499:	0f 1f 80 00 00 00 00 	nopl   0x0(%rax)

00000000004004a0 <register_tm_clones>:
  4004a0:	b8 38 10 60 00       	mov    $0x601038,%eax
  4004a5:	55                   	push   %rbp
  4004a6:	48 2d 38 10 60 00    	sub    $0x601038,%rax
  4004ac:	48 c1 f8 03          	sar    $0x3,%rax
  4004b0:	48 89 e5             	mov    %rsp,%rbp
  4004b3:	48 89 c2             	mov    %rax,%rdx
  4004b6:	48 c1 ea 3f          	shr    $0x3f,%rdx
  4004ba:	48 01 d0             	add    %rdx,%rax
  4004bd:	48 d1 f8             	sar    %rax
  4004c0:	75 02                	jne    4004c4 <register_tm_clones+0x24>
  4004c2:	5d                   	pop    %rbp
  4004c3:	c3                   	retq   
  4004c4:	ba 00 00 00 00       	mov    $0x0,%edx
  4004c9:	48 85 d2             	test   %rdx,%rdx
  4004cc:	74 f4                	je     4004c2 <register_tm_clones+0x22>
  4004ce:	5d                   	pop    %rbp
  4004cf:	48 89 c6             	mov    %rax,%rsi
  4004d2:	bf 38 10 60 00       	mov    $0x601038,%edi
  4004d7:	ff e2                	jmpq   *%rdx
  4004d9:	0f 1f 80 00 00 00 00 	nopl   0x0(%rax)

00000000004004e0 <__do_global_dtors_aux>:
  4004e0:	80 3d 4d 0b 20 00 00 	cmpb   $0x0,0x200b4d(%rip)        # 601034 <_edata>
  4004e7:	75 11                	jne    4004fa <__do_global_dtors_aux+0x1a>
  4004e9:	55                   	push   %rbp
  4004ea:	48 89 e5             	mov    %rsp,%rbp
  4004ed:	e8 7e ff ff ff       	callq  400470 <deregister_tm_clones>
  4004f2:	5d                   	pop    %rbp
  4004f3:	c6 05 3a 0b 20 00 01 	movb   $0x1,0x200b3a(%rip)        # 601034 <_edata>
  4004fa:	f3 c3                	repz retq 
  4004fc:	0f 1f 40 00          	nopl   0x0(%rax)

0000000000400500 <frame_dummy>:
  400500:	48 83 3d 18 09 20 00 	cmpq   $0x0,0x200918(%rip)        # 600e20 <__JCR_END__>
  400507:	00 
  400508:	74 1e                	je     400528 <frame_dummy+0x28>
  40050a:	b8 00 00 00 00       	mov    $0x0,%eax
  40050f:	48 85 c0             	test   %rax,%rax
  400512:	74 14                	je     400528 <frame_dummy+0x28>
  400514:	55                   	push   %rbp
  400515:	bf 20 0e 60 00       	mov    $0x600e20,%edi
  40051a:	48 89 e5             	mov    %rsp,%rbp
  40051d:	ff d0                	callq  *%rax
  40051f:	5d                   	pop    %rbp
  400520:	e9 7b ff ff ff       	jmpq   4004a0 <register_tm_clones>
  400525:	0f 1f 00             	nopl   (%rax)
  400528:	e9 73 ff ff ff       	jmpq   4004a0 <register_tm_clones>
  40052d:	0f 1f 00             	nopl   (%rax)

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

int main() {
  400530:	55                   	push   %rbp
  400531:	48 89 e5             	mov    %rsp,%rbp
    printf("Hello World!\n");
  400534:	bf e0 05 40 00       	mov    $0x4005e0,%edi
  400539:	b8 00 00 00 00       	mov    $0x0,%eax
  40053e:	e8 cd fe ff ff       	callq  400410 <printf@plt>
}
  400543:	5d                   	pop    %rbp
  400544:	c3                   	retq   
  400545:	66 2e 0f 1f 84 00 00 	nopw   %cs:0x0(%rax,%rax,1)
  40054c:	00 00 00 
  40054f:	90                   	nop

0000000000400550 <__libc_csu_init>:
  400550:	41 57                	push   %r15
  400552:	41 89 ff             	mov    %edi,%r15d
  400555:	41 56                	push   %r14
  400557:	49 89 f6             	mov    %rsi,%r14
  40055a:	41 55                	push   %r13
  40055c:	49 89 d5             	mov    %rdx,%r13
  40055f:	41 54                	push   %r12
  400561:	4c 8d 25 a8 08 20 00 	lea    0x2008a8(%rip),%r12        # 600e10 <__frame_dummy_init_array_entry>
  400568:	55                   	push   %rbp
  400569:	48 8d 2d a8 08 20 00 	lea    0x2008a8(%rip),%rbp        # 600e18 <__init_array_end>
  400570:	53                   	push   %rbx
  400571:	4c 29 e5             	sub    %r12,%rbp
  400574:	31 db                	xor    %ebx,%ebx
  400576:	48 c1 fd 03          	sar    $0x3,%rbp
  40057a:	48 83 ec 08          	sub    $0x8,%rsp
  40057e:	e8 5d fe ff ff       	callq  4003e0 <_init>
  400583:	48 85 ed             	test   %rbp,%rbp
  400586:	74 1e                	je     4005a6 <__libc_csu_init+0x56>
  400588:	0f 1f 84 00 00 00 00 	nopl   0x0(%rax,%rax,1)
  40058f:	00 
  400590:	4c 89 ea             	mov    %r13,%rdx
  400593:	4c 89 f6             	mov    %r14,%rsi
  400596:	44 89 ff             	mov    %r15d,%edi
  400599:	41 ff 14 dc          	callq  *(%r12,%rbx,8)
  40059d:	48 83 c3 01          	add    $0x1,%rbx
  4005a1:	48 39 eb             	cmp    %rbp,%rbx
  4005a4:	75 ea                	jne    400590 <__libc_csu_init+0x40>
  4005a6:	48 83 c4 08          	add    $0x8,%rsp
  4005aa:	5b                   	pop    %rbx
  4005ab:	5d                   	pop    %rbp
  4005ac:	41 5c                	pop    %r12
  4005ae:	41 5d                	pop    %r13
  4005b0:	41 5e                	pop    %r14
  4005b2:	41 5f                	pop    %r15
  4005b4:	c3                   	retq   
  4005b5:	66 66 2e 0f 1f 84 00 	data32 nopw %cs:0x0(%rax,%rax,1)
  4005bc:	00 00 00 00 

00000000004005c0 <__libc_csu_fini>:
  4005c0:	f3 c3                	repz retq 
  4005c2:	66 90                	xchg   %ax,%ax

Disassembly of section .fini:

00000000004005c4 <_fini>:
  4005c4:	48 83 ec 08          	sub    $0x8,%rsp
  4005c8:	48 83 c4 08          	add    $0x8,%rsp
  4005cc:	c3                   	retq  

Looking at this, we can see that the source code for this file can be found in the ‘<main>’ section.

###The String The string output looks like it can be found in the ‘.rodata’ section of the output that can be seen using the -s flag.

[hunter@li62-155 SPO600]$ objdump -s a.out 

a.out:     file format elf64-x86-64

Contents of section .interp:
 400238 2f6c6962 36342f6c 642d6c69 6e75782d  /lib64/ld-linux-
 400248 7838362d 36342e73 6f2e3200           x86-64.so.2.    
Contents of section .note.ABI-tag:
 400254 04000000 10000000 01000000 474e5500  ............GNU.
 400264 00000000 02000000 06000000 20000000  ............ ...
Contents of section .note.gnu.build-id:
 400274 04000000 14000000 03000000 474e5500  ............GNU.
 400284 87d14922 11734b7a 54671f88 075e22ff  ..I".sKzTg...^".
 400294 8d8851d5                             ..Q.            
Contents of section .gnu.hash:
 400298 01000000 01000000 01000000 00000000  ................
 4002a8 00000000 00000000 00000000           ............    
Contents of section .dynsym:
 4002b8 00000000 00000000 00000000 00000000  ................
 4002c8 00000000 00000000 0b000000 12000000  ................
 4002d8 00000000 00000000 00000000 00000000  ................
 4002e8 12000000 12000000 00000000 00000000  ................
 4002f8 00000000 00000000 24000000 20000000  ........$... ...
 400308 00000000 00000000 00000000 00000000  ................
Contents of section .dynstr:
 400318 006c6962 632e736f 2e360070 72696e74  .libc.so.6.print
 400328 66005f5f 6c696263 5f737461 72745f6d  f.__libc_start_m
 400338 61696e00 5f5f676d 6f6e5f73 74617274  ain.__gmon_start
 400348 5f5f0047 4c494243 5f322e32 2e3500    __.GLIBC_2.2.5. 
Contents of section .gnu.version:
 400358 00000200 02000000                    ........        
Contents of section .gnu.version_r:
 400360 01000100 01000000 10000000 00000000  ................
 400370 751a6909 00000200 33000000 00000000  u.i.....3.......
Contents of section .rela.dyn:
 400380 f80f6000 00000000 06000000 03000000  ..`.............
 400390 00000000 00000000                    ........        
Contents of section .rela.plt:
 400398 18106000 00000000 07000000 01000000  ..`.............
 4003a8 00000000 00000000 20106000 00000000  ........ .`.....
 4003b8 07000000 02000000 00000000 00000000  ................
 4003c8 28106000 00000000 07000000 03000000  (.`.............
 4003d8 00000000 00000000                    ........        
Contents of section .init:
 4003e0 4883ec08 488b050d 0c200048 85c07405  H...H.... .H..t.
 4003f0 e83b0000 004883c4 08c3               .;...H....      
Contents of section .plt:
 400400 ff35020c 2000ff25 040c2000 0f1f4000  .5.. ..%.. ...@.
 400410 ff25020c 20006800 000000e9 e0ffffff  .%.. .h.........
 400420 ff25fa0b 20006801 000000e9 d0ffffff  .%.. .h.........
 400430 ff25f20b 20006802 000000e9 c0ffffff  .%.. .h.........
Contents of section .text:
 400440 31ed4989 d15e4889 e24883e4 f0505449  1.I..^H..H...PTI
 400450 c7c0c005 400048c7 c1500540 0048c7c7  ....@.H..P.@.H..
 400460 30054000 e8b7ffff fff46690 0f1f4000  0.@.......f...@.
 400470 b83f1060 0055482d 38106000 4883f80e  .?.`.UH-8.`.H...
 400480 4889e577 025dc3b8 00000000 4885c074  H..w.]......H..t
 400490 f45dbf38 106000ff e00f1f80 00000000  .].8.`..........
 4004a0 b8381060 0055482d 38106000 48c1f803  .8.`.UH-8.`.H...
 4004b0 4889e548 89c248c1 ea3f4801 d048d1f8  H..H..H..?H..H..
 4004c0 75025dc3 ba000000 004885d2 74f45d48  u.]......H..t.]H
 4004d0 89c6bf38 106000ff e20f1f80 00000000  ...8.`..........
 4004e0 803d4d0b 20000075 11554889 e5e87eff  .=M. ..u.UH...~.
 4004f0 ffff5dc6 053a0b20 0001f3c3 0f1f4000  ..]..:. ......@.
 400500 48833d18 09200000 741eb800 00000048  H.=.. ..t......H
 400510 85c07414 55bf200e 60004889 e5ffd05d  ..t.U. .`.H....]
 400520 e97bffff ff0f1f00 e973ffff ff0f1f00  .{.......s......
 400530 554889e5 bfe00540 00b80000 0000e8cd  UH.....@........
 400540 feffff5d c3662e0f 1f840000 00000090  ...].f..........
 400550 41574189 ff415649 89f64155 4989d541  AWA..AVI..AUI..A
 400560 544c8d25 a8082000 55488d2d a8082000  TL.%.. .UH.-.. .
 400570 534c29e5 31db48c1 fd034883 ec08e85d  SL).1.H...H....]
 400580 feffff48 85ed741e 0f1f8400 00000000  ...H..t.........
 400590 4c89ea4c 89f64489 ff41ff14 dc4883c3  L..L..D..A...H..
 4005a0 014839eb 75ea4883 c4085b5d 415c415d  .H9.u.H...[]A\A]
 4005b0 415e415f c366662e 0f1f8400 00000000  A^A_.ff.........
 4005c0 f3c36690                             ..f.            
Contents of section .fini:
 4005c4 4883ec08 4883c408 c3                 H...H....       
Contents of section .rodata:
 4005d0 01000200 00000000 00000000 00000000  ................
 4005e0 48656c6c 6f20576f 726c6421 0a00      Hello World!..  
Contents of section .eh_frame_hdr:
 4005f0 011b033b 34000000 05000000 10feffff  ...;4...........
 400600 80000000 50feffff 50000000 40ffffff  ....P...P...@...
 400610 a8000000 60ffffff c8000000 d0ffffff  ....`...........
 400620 10010000                             ....            
Contents of section .eh_frame:
 400628 14000000 00000000 017a5200 01781001  .........zR..x..
 400638 1b0c0708 90010710 14000000 1c000000  ................
 400648 f8fdffff 2a000000 00000000 00000000  ....*...........
 400658 14000000 00000000 017a5200 01781001  .........zR..x..
 400668 1b0c0708 90010000 24000000 1c000000  ........$.......
 400678 88fdffff 40000000 000e1046 0e184a0f  ....@......F..J.
 400688 0b770880 003f1a3b 2a332422 00000000  .w...?.;*3$"....
 400698 1c000000 44000000 90feffff 15000000  ....D...........
 4006a8 00410e10 8602430d 06500c07 08000000  .A....C..P......
 4006b8 44000000 64000000 90feffff 65000000  D...d.......e...
 4006c8 00420e10 8f02450e 188e0345 0e208d04  .B....E....E. ..
 4006d8 450e288c 05480e30 8606480e 3883074d  E.(..H.0..H.8..M
 4006e8 0e406c0e 38410e30 410e2842 0e20420e  .@l.8A.0A.(B. B.
 4006f8 18420e10 420e0800 14000000 ac000000  .B..B...........
 400708 b8feffff 02000000 00000000 00000000  ................
 400718 00000000                             ....            
Contents of section .init_array:
 600e10 00054000 00000000                    ..@.....        
Contents of section .fini_array:
 600e18 e0044000 00000000                    ..@.....        
Contents of section .jcr:
 600e20 00000000 00000000                    ........        
Contents of section .dynamic:
 600e28 01000000 00000000 01000000 00000000  ................
 600e38 0c000000 00000000 e0034000 00000000  ..........@.....
 600e48 0d000000 00000000 c4054000 00000000  ..........@.....
 600e58 19000000 00000000 100e6000 00000000  ..........`.....
 600e68 1b000000 00000000 08000000 00000000  ................
 600e78 1a000000 00000000 180e6000 00000000  ..........`.....
 600e88 1c000000 00000000 08000000 00000000  ................
 600e98 f5feff6f 00000000 98024000 00000000  ...o......@.....
 600ea8 05000000 00000000 18034000 00000000  ..........@.....
 600eb8 06000000 00000000 b8024000 00000000  ..........@.....
 600ec8 0a000000 00000000 3f000000 00000000  ........?.......
 600ed8 0b000000 00000000 18000000 00000000  ................
 600ee8 15000000 00000000 00000000 00000000  ................
 600ef8 03000000 00000000 00106000 00000000  ..........`.....
 600f08 02000000 00000000 48000000 00000000  ........H.......
 600f18 14000000 00000000 07000000 00000000  ................
 600f28 17000000 00000000 98034000 00000000  ..........@.....
 600f38 07000000 00000000 80034000 00000000  ..........@.....
 600f48 08000000 00000000 18000000 00000000  ................
 600f58 09000000 00000000 18000000 00000000  ................
 600f68 feffff6f 00000000 60034000 00000000  ...o....`.@.....
 600f78 ffffff6f 00000000 01000000 00000000  ...o............
 600f88 f0ffff6f 00000000 58034000 00000000  ...o....X.@.....
 600f98 00000000 00000000 00000000 00000000  ................
 600fa8 00000000 00000000 00000000 00000000  ................
 600fb8 00000000 00000000 00000000 00000000  ................
 600fc8 00000000 00000000 00000000 00000000  ................
 600fd8 00000000 00000000 00000000 00000000  ................
 600fe8 00000000 00000000 00000000 00000000  ................
Contents of section .got:
 600ff8 00000000 00000000                    ........        
Contents of section .got.plt:
 601000 280e6000 00000000 00000000 00000000  (.`.............
 601010 00000000 00000000 16044000 00000000  ..........@.....
 601020 26044000 00000000 36044000 00000000  &.@.....6.@.....
Contents of section .data:
 601030 00000000                             ....            
Contents of section .comment:
 0000 4743433a 2028474e 55292034 2e382e33  GCC: (GNU) 4.8.3
 0010 20323031 34303632 34202852 65642048   20140624 (Red H
 0020 61742034 2e382e33 2d312900           at 4.8.3-1).    
Contents of section .debug_aranges:
 0000 2c000000 02000000 00000800 00000000  ,...............
 0010 30054000 00000000 15000000 00000000  0.@.............
 0020 00000000 00000000 00000000 00000000  ................
Contents of section .debug_info:
 0000 8d000000 04000000 00000801 07000000  ................
 0010 01000000 00b30000 00300540 00000000  .........0.@....
 0020 00150000 00000000 00000000 00020807  ................
 0030 a1000000 0201087c 00000002 02075f00  .......|......_.
 0040 00000204 07a60000 00020106 7e000000  ............~...
 0050 02020572 00000003 0405696e 74000208  ...r......int...
 0060 058a0000 00020807 93000000 02010685  ................
 0070 00000004 9c000000 01035700 00003005  ..........W...0.
 0080 40000000 00001500 00000000 0000019c  @...............
 0090 00                                   .               
Contents of section .debug_abbrev:
 0000 01110125 0e130b03 0e1b0e11 01120710  ...%............
 0010 17000002 24000b0b 3e0b030e 00000324  ....$...>......$
 0020 000b0b3e 0b030800 00042e00 3f19030e  ...>........?...
 0030 3a0b3b0b 49131101 12074018 96421900  :.;.I.....@..B..
 0040 0000                                 ..              
Contents of section .debug_line:
 0000 36000000 02001d00 00000101 fb0e0d00  6...............
 0010 01010101 00000001 00000100 6c616232  ............lab2
 0020 2e630000 00000000 09023005 40000000  .c........0.@...
 0030 0000144b e5020200 0101               ...K......      
Contents of section .debug_str:
 0000 6c616232 2e630047 4e552043 20342e38  lab2.c.GNU C 4.8
 0010 2e332032 30313430 36323420 28526564  .3 20140624 (Red
 0020 20486174 20342e38 2e332d31 29202d6d   Hat 4.8.3-1) -m
 0030 74756e65 3d67656e 65726963 202d6d61  tune=generic -ma
 0040 7263683d 7838362d 3634202d 67202d4f  rch=x86-64 -g -O
 0050 30202d66 6e6f2d62 75696c74 696e0073  0 -fno-builtin.s
 0060 686f7274 20756e73 69676e65 6420696e  hort unsigned in
 0070 74007368 6f727420 696e7400 756e7369  t.short int.unsi
 0080 676e6564 20636861 72006c6f 6e672069  gned char.long i
 0090 6e740073 697a6574 79706500 6d61696e  nt.sizetype.main
 00a0 006c6f6e 6720756e 7369676e 65642069  .long unsigned i
 00b0 6e74002f 686f6d65 2f68756e 7465722f  nt./home/hunter/
 00c0 53504f36 303000                      SPO600.   

###Recompiling After looking at the objdump data and trying to figure out what’s going on, it’s time to recompile with the added flag -static.

ls -l of the previous version:

-rwxr-xr-x 1 hunter wheel 9528 Sep 13 19:08 a.out

and after adding the static flag:

-rwxr-xr-x 1 hunter wheel 812521 Sep 16 20:10 a.out

We can quite clearly see that by compiling with the static call ends up in a much larger file. But why? Well, from what I can understand, it’s because instead when we add static, it no longer links to the external library needed for the program (stdio for printf), but instead has to statically embed that library into our program.

With that in mind, let’s compare a couple of the other sections. Due to the static linking, it makes looking through the various objdump values a lot more bloated than without statically linking it. So I’ll try to only grab relevant comparison bits.

###The Source

now looks like this: ``` 0000000000400e30
: #include int main() { 400e30: 55 push %rbp 400e31: 48 89 e5 mov %rsp,%rbp printf("Hello World!\n"); 400e34: bf 70 9a 48 00 mov $0x489a70,%edi 400e39: b8 00 00 00 00 mov $0x0,%eax 400e3e: e8 1d 0a 00 00 callq 401860 <_IO_printf> } 400e43: 5d pop %rbp 400e44: c3 retq 400e45: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1) 400e4c: 00 00 00 400e4f: 90 nop ``` If you compare to the previous version...It's the same...Almost! The only difference that I can notice is that the call to '<printf@plt>' is replaced to the call '<_IO_printf>'. ###Recompiling After looking at the objdump data and puzzling over it some more, it's time to recompile without the -fno-builtin. I kept the static flag though. ls -l of the new version: ``` -rwxr-xr-x 1 hunter wheel 812580 Sep 16 22:36 a.out ``` It's even bigger! Not by too much, but it's still bigger. ###The Source Why might this be? From what I gather, it's because instead of using built in system calls, it now uses GCC's optimized functions that do the same, but perhaps more efficiently. The current version of main looks like this: ``` 0000000000400e30
: #include int main() { 400e30: 55 push %rbp 400e31: 48 89 e5 mov %rsp,%rbp printf("Hello World!\n"); 400e34: bf b0 9b 48 00 mov $0x489bb0,%edi 400e39: e8 12 0a 00 00 callq 401850 <_IO_puts> } 400e3e: 5d pop %rbp 400e3f: c3 retq ``` As you can see - it's three lines shorter and instead of calling IO_PRINT, it's not calling IO_PUTS! ###Recompiling After poking at the objdump data and getting a headache, it's time to recompile without the -g. I also left -fno-builtin though. I kept the static flag though. ls -l of the new version: ``` -rwxr-xr-x 1 hunter wheel 811580 Sep 16 22:47 a.out ``` It's smaller! But why on earth is that?! ###The Source The -g flag adds debugging information in the operating system’s native format. So it makes sense that by excluding that flag the file size is smaller. It would make sense to include this flag when developing, but not when producing a final product. ###Changing it up Alright, so now we need to add some arugments to the printf statement and see what's up. The source in our file is now: ``` #include int main() { printf("Hello World! %d, %d, %d, %d, %d, %d, %d, %d, %d, %d\n", 5, 10, 15, 20, 25, 30, 35, 40, 45, 50); } ``` This time I'm compiling with: 'gcc -g -O0 -fno-builtin lab2.c' ###The Source Our main now looks like this: ``` 0000000000400530
: #include int main() { 400530: 55 push %rbp 400531: 48 89 e5 mov %rsp,%rbp 400534: 48 83 ec 30 sub $0x30,%rsp printf("Hello World! %d, %d, %d, %d, %d, %d, %d, %d, %d, %d\n", 5, 10, 15, 20, 25, 30, 35, 40, 45, 50); 400538: c7 44 24 20 32 00 00 movl $0x32,0x20(%rsp) 40053f: 00 400540: c7 44 24 18 2d 00 00 movl $0x2d,0x18(%rsp) 400547: 00 400548: c7 44 24 10 28 00 00 movl $0x28,0x10(%rsp) 40054f: 00 400550: c7 44 24 08 23 00 00 movl $0x23,0x8(%rsp) 400557: 00 400558: c7 04 24 1e 00 00 00 movl $0x1e,(%rsp) 40055f: 41 b9 19 00 00 00 mov $0x19,%r9d 400565: 41 b8 14 00 00 00 mov $0x14,%r8d 40056b: b9 0f 00 00 00 mov $0xf,%ecx 400570: ba 0a 00 00 00 mov $0xa,%edx 400575: be 05 00 00 00 mov $0x5,%esi 40057a: bf 20 06 40 00 mov $0x400620,%edi 40057f: b8 00 00 00 00 mov $0x0,%eax 400584: e8 87 fe ff ff callq 400410 <printf@plt> } 400589: c9 leaveq 40058a: c3 retq 40058b: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1) ``` compare this to the original: ``` 0000000000400530
: #include int main() { 400530: 55 push %rbp 400531: 48 89 e5 mov %rsp,%rbp printf("Hello World!\n"); 400534: bf e0 05 40 00 mov $0x4005e0,%edi 400539: b8 00 00 00 00 mov $0x0,%eax 40053e: e8 cd fe ff ff callq 400410 <printf@plt> } 400543: 5d pop %rbp 400544: c3 retq 400545: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1) 40054c: 00 00 00 40054f: 90 nop ``` And we'll notice an extra 10 move commands occuring; funnily enough we have 10 variables in our printf. ###Switching it up - Again! So now, I'm moving the print to a new function and calling that function from main. ``` #include int main() { saythething(); } void saythething(){ printf("Hello World! %d, %d, %d, %d, %d, %d, %d, %d, %d, %d\n", 5, 10, 15, 20, 25, 30, 35, 40, 45, 50); } ``` I compiled with the same command as last time: I did get some warnings, but I'm ignoring them. ###The source Now along with the
section we should take note of the section. 0000000000400530
: #include ``` int main() { 400530: 55 push %rbp 400531: 48 89 e5 mov %rsp,%rbp saythething(); 400534: b8 00 00 00 00 mov $0x0,%eax 400539: e8 02 00 00 00 callq 400540 } 40053e: 5d pop %rbp 40053f: c3 retq 0000000000400540 : void saythething(){ 400540: 55 push %rbp 400541: 48 89 e5 mov %rsp,%rbp 400544: 48 83 ec 30 sub $0x30,%rsp printf("Hello World! %d, %d, %d, %d, %d, %d, %d, %d, %d, %d\n", 5, 10, 15, 20, 25, 30, 35, 40, 45, 50); 400548: c7 44 24 20 32 00 00 movl $0x32,0x20(%rsp) 40054f: 00 400550: c7 44 24 18 2d 00 00 movl $0x2d,0x18(%rsp) 400557: 00 400558: c7 44 24 10 28 00 00 movl $0x28,0x10(%rsp) 40055f: 00 400560: c7 44 24 08 23 00 00 movl $0x23,0x8(%rsp) 400567: 00 400568: c7 04 24 1e 00 00 00 movl $0x1e,(%rsp) 40056f: 41 b9 19 00 00 00 mov $0x19,%r9d 400575: 41 b8 14 00 00 00 mov $0x14,%r8d 40057b: b9 0f 00 00 00 mov $0xf,%ecx 400580: ba 0a 00 00 00 mov $0xa,%edx 400585: be 05 00 00 00 mov $0x5,%esi 40058a: bf 30 06 40 00 mov $0x400630,%edi 40058f: b8 00 00 00 00 mov $0x0,%eax 400594: e8 77 fe ff ff callq 400410 <printf@plt> } 400599: c9 leaveq 40059a: c3 retq 40059b: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1) ``` We can see that in the main section, we now have a call command to the saythething section and the saythething section looks mostly like our old main. And that makes sense. ###One Final Change So for our final thing, we're going to remove the optimizing flag -O0 and add -O3 to the gcc options. I'll keep the code the same. ``` [hunter@li62-155 SPO600]$ gcc -g -O3 -fno-builtin lab2.c ``` ###The Source - ONE LAST TIME Things are in a different order this time! Previously, the main and saythething section were in order. This time, the main section is at the top and looks like: ``` 0000000000400440
: #include int main() { saythething(); 400440: 31 c0 xor %eax,%eax 400442: e9 f9 00 00 00 jmpq 400540 400447: 90 ``` and a bit further down, the saythething section ``` void saythething(){ 400540: 48 83 ec 38 sub $0x38,%rsp printf("Hello World! %d, %d, %d, %d, %d, %d, %d, %d, %d, %d\n", 5, 10, 15, 20, 25, 30, 35, 40, 45, 50); 400544: 41 b9 19 00 00 00 mov $0x19,%r9d 40054a: 41 b8 14 00 00 00 mov $0x14,%r8d 400550: c7 44 24 20 32 00 00 movl $0x32,0x20(%rsp) 400557: 00 400558: c7 44 24 18 2d 00 00 movl $0x2d,0x18(%rsp) 40055f: 00 400560: b9 0f 00 00 00 mov $0xf,%ecx 400565: c7 44 24 10 28 00 00 movl $0x28,0x10(%rsp) 40056c: 00 40056d: c7 44 24 08 23 00 00 movl $0x23,0x8(%rsp) 400574: 00 400575: ba 0a 00 00 00 mov $0xa,%edx 40057a: c7 04 24 1e 00 00 00 movl $0x1e,(%rsp) 400581: be 05 00 00 00 mov $0x5,%esi 400586: bf 30 06 40 00 mov $0x400630,%edi 40058b: 31 c0 xor %eax,%eax 40058d: e8 7e fe ff ff callq 400410 <printf@plt> } 400592: 48 83 c4 38 add $0x38,%rsp 400596: c3 retq 400597: 66 0f 1f 84 00 00 00 nopw 0x0(%rax,%rax,1) 40059e: 00 00 ``` These look different compared to the previous versions. This version has xor instead of push and jmpq instead of mov. There's also no 'pop' or 'leaveq' commands. While, I certainly don't pretend to understand everything that was shown here - this step by step walk through of compiled output has begun the process to comprehending the bananas world of assembler! Until Next time -Hunter