Compiled C Lab
Server used in this lab is Aarch64
$ ssh siwen@aarchie.cdot.systems
Step 1 Write a basic C program which prints a message on the screen
#include <stdio.h>
int main() {
printf ("Hello World!\n");
}
Step 2 Compile the program using the GCC compiler
$ gcc hello.c -o hello0 -g -O0 -fno-builtin && ll -h
-g # enable debugging information
-O0 # do not optimize (that's a capital letter and then the digit zero)
-fno-builtin # do not use builtin function optimization
ll -h # displays detail info of all files/directories with names starting with letter h
Step 3 Using objdump
$ objdump hello0 -f -s -d | less
-f # display header information for the entire file
-s # display per-section summary information
-d # disassemble sections containing code
less # enable pageup and pagedown
Find the answers to these questions:
(i) Which section contains the code you wrote? .text?
(ii) Which section contains the string to be printed? .rodata
Step 4 Recompile the code with these changes
0: The original one
72k
0000000000400594 <main>:
400594: a9bf7bfd stp x29, x30, [sp, #-16]!
400598: 910003fd mov x29, sp
40059c: 90000000 adrp x0, 400000 <_init-0x418>
4005a0: 9119c000 add x0, x0, #0x670
4005a4: 97ffffb7 bl 400480 <printf@plt>
4005a8: 52800000 mov w0, #0x0 // #0
4005ac: a8c17bfd ldp x29, x30, [sp], #16
4005b0: d65f03c0 ret
4005b4: 00000000 .inst 0x00000000 ; undefined
[explanation] This explanation below is written by Ryan Marzec. Click here to find the original blog.
push %rbp(register base pointer, start of stack) onto the stack
move data from %rsp(register stack pointer, current location in stack) to %rbp
– this is setting the current location of the stack the base, or start of the stack.
move $0x402010 (this is the address of the string containing “Hello World!\n”) to %edi (register destination index, destination of data copies)
move %0x0 (the value of zero) into %eax(register z extended)
– eax is basically storing a function return type
call the printf function.
– @plt here is for Procedure Linking Table
– <printf@plt> is essentially a small stub that will call the real printf function from an external shared library
this move is for the return for main
pop %rbp from the stack
return
1: Add the compiler option
$ gcc hello.c -o hello1 -g -O0 -fno-builtin -static && ll -h
This option disables the use of dynamic libraries, so what is compiled is generally large and doesn't need Dynamic connection library when run.
617k
contain lots of things like every letter from alphebta ,every month...
00000000004005e4 <main>:
4005e4: a9bf7bfd stp x29, x30, [sp, #-16]!
4005e8: 910003fd mov x29, sp
4005ec: f0000260 adrp x0, 44f000 <free_mem+0x10>
4005f0: 910f0000 add x0, x0, #0x3c0
4005f4: 9400182b bl 4066a0 <_IO_printf>
4005f8: 52800000 mov w0, #0x0 // #0
4005fc: a8c17bfd ldp x29, x30, [sp], #16
400600: d65f03c0 ret
400604: 00000000 .inst 0x00000000 ; undefined
2: Remove the compiler option -fno-builtin
$ gcc hello.c -o hello2 -g -O0 && ll -h
Contents of section .debug_str:
0210 6465005f 494f5f72 6561645f 62617365 de._IO_read_base
0220 00737973 5f6e6572 72005f76 7461626c .sys_nerr._vtabl
0230 655f6f66 66736574 005f494f 5f736176 e_offset._IO_sav
0240 655f6261 73650073 79735f65 72726c69 e_base.sys_errli
0250 7374005f 66696c65 6e6f0068 656c6c6f st._fileno.hello
0260 2e630073 74646f75 74005f49 4f5f325f .c.stdout._IO_2_
0270 315f7374 646f7574 5f005f49 4f5f6c6f 1_stdout_._IO_lo
0280 636b5f74 00 ck_t.
3: Remove the compiler option -g
$ gcc hello.c -o hello3 -O0 -fno-builtin && ll -h
no debug section
4: Add additional argument to the printf() function in your program
#include <stdio.h>
int main() {
printf ("Hello World! %d\n", 18);
}
Contents of section .eh_frame_hdr:
Contents of section .debug_str:
new line: 40059c: 52800241 mov w1, #0x12 // #18
4005b4: 00000000 .inst 0x00000000 ; undefined
5: Move the printf() call to a separate function named output(), and call that function from main()
#include <stdio.h>
void message(char str[]) {
printf(str);
}
int main() {
message("Hello World!\n");
}
Contents of section .text:
Contents of section .debug_info:
0000000000400594 <message>:
400594: a9be7bfd stp x29, x30, [sp, #-32]!
400598: 910003fd mov x29, sp
40059c: f9000fe0 str x0, [sp, #24]
4005a0: f9400fe0 ldr x0, [sp, #24]
4005a4: 97ffffb7 bl 400480 <printf@plt>
4005a8: d503201f nop
4005ac: a8c27bfd ldp x29, x30, [sp], #32
4005b0: d65f03c0 ret
00000000004005b4 <main>:
4005b4: a9bf7bfd stp x29, x30, [sp, #-16]!
4005b8: 910003fd mov x29, sp
4005bc: 90000000 adrp x0, 400000 <_init-0x418>
4005c0: 911a4000 add x0, x0, #0x690
4005c4: 97fffff4 bl 400594 <message>
4005c8: 52800000 mov w0, #0x0 // #0
4005cc: a8c17bfd ldp x29, x30, [sp], #16
4005d0: d65f03c0 ret
4005d4: 00000000 .inst 0x00000000 ; undefined
6: Remove -O0 and add -O3 to the gcc options
$ gcc hello.c -o hello0 -g -O3 -fno-builtin && ll -h
-O3 optimized, including the inline function.
Contents of section .eh_frame_hdr:
Contents of section .debug_info:
Contents of section .debug_str:
Contents of section .debug_ranges:
0000 90044000 00000000 b0044000 00000000 ..@.......@.....
0010 00000000 00000000 00000000 00000000 ................
<main> part 1st position
$ ssh siwen@aarchie.cdot.systems
Step 1 Write a basic C program which prints a message on the screen
#include <stdio.h>
int main() {
printf ("Hello World!\n");
}
Step 2 Compile the program using the GCC compiler
$ gcc hello.c -o hello0 -g -O0 -fno-builtin && ll -h
-g # enable debugging information
-O0 # do not optimize (that's a capital letter and then the digit zero)
-fno-builtin # do not use builtin function optimization
ll -h # displays detail info of all files/directories with names starting with letter h
Step 3 Using objdump
$ objdump hello0 -f -s -d | less
-f # display header information for the entire file
-s # display per-section summary information
-d # disassemble sections containing code
less # enable pageup and pagedown
Find the answers to these questions:
(i) Which section contains the code you wrote? .text?
(ii) Which section contains the string to be printed? .rodata
Step 4 Recompile the code with these changes
0: The original one
72k
0000000000400594 <main>:
400594: a9bf7bfd stp x29, x30, [sp, #-16]!
400598: 910003fd mov x29, sp
40059c: 90000000 adrp x0, 400000 <_init-0x418>
4005a0: 9119c000 add x0, x0, #0x670
4005a4: 97ffffb7 bl 400480 <printf@plt>
4005a8: 52800000 mov w0, #0x0 // #0
4005ac: a8c17bfd ldp x29, x30, [sp], #16
4005b0: d65f03c0 ret
4005b4: 00000000 .inst 0x00000000 ; undefined
[explanation] This explanation below is written by Ryan Marzec. Click here to find the original blog.
push %rbp(register base pointer, start of stack) onto the stack
move data from %rsp(register stack pointer, current location in stack) to %rbp
– this is setting the current location of the stack the base, or start of the stack.
move $0x402010 (this is the address of the string containing “Hello World!\n”) to %edi (register destination index, destination of data copies)
move %0x0 (the value of zero) into %eax(register z extended)
– eax is basically storing a function return type
call the printf function.
– @plt here is for Procedure Linking Table
– <printf@plt> is essentially a small stub that will call the real printf function from an external shared library
this move is for the return for main
pop %rbp from the stack
return
1: Add the compiler option
-static$ gcc hello.c -o hello1 -g -O0 -fno-builtin -static && ll -h
This option disables the use of dynamic libraries, so what is compiled is generally large and doesn't need Dynamic connection library when run.
617k
contain lots of things like every letter from alphebta ,every month...
00000000004005e4 <main>:
4005e4: a9bf7bfd stp x29, x30, [sp, #-16]!
4005e8: 910003fd mov x29, sp
4005ec: f0000260 adrp x0, 44f000 <free_mem+0x10>
4005f0: 910f0000 add x0, x0, #0x3c0
4005f4: 9400182b bl 4066a0 <_IO_printf>
4005f8: 52800000 mov w0, #0x0 // #0
4005fc: a8c17bfd ldp x29, x30, [sp], #16
400600: d65f03c0 ret
400604: 00000000 .inst 0x00000000 ; undefined
2: Remove the compiler option -fno-builtin
$ gcc hello.c -o hello2 -g -O0 && ll -h
Contents of section .debug_str:
0210 6465005f 494f5f72 6561645f 62617365 de._IO_read_base
0220 00737973 5f6e6572 72005f76 7461626c .sys_nerr._vtabl
0230 655f6f66 66736574 005f494f 5f736176 e_offset._IO_sav
0240 655f6261 73650073 79735f65 72726c69 e_base.sys_errli
0250 7374005f 66696c65 6e6f0068 656c6c6f st._fileno.hello
0260 2e630073 74646f75 74005f49 4f5f325f .c.stdout._IO_2_
0270 315f7374 646f7574 5f005f49 4f5f6c6f 1_stdout_._IO_lo
0280 636b5f74 00 ck_t.
3: Remove the compiler option -g
$ gcc hello.c -o hello3 -O0 -fno-builtin && ll -h
no debug section
4: Add additional argument to the printf() function in your program
#include <stdio.h>
int main() {
printf ("Hello World! %d\n", 18);
}
Contents of section .eh_frame_hdr:
Contents of section .debug_str:
new line: 40059c: 52800241 mov w1, #0x12 // #18
5: Move the printf() call to a separate function named output(), and call that function from main()
#include <stdio.h>
void message(char str[]) {
printf(str);
}
int main() {
message("Hello World!\n");
}
Contents of section .text:
Contents of section .debug_info:
0000000000400594 <message>:
400594: a9be7bfd stp x29, x30, [sp, #-32]!
400598: 910003fd mov x29, sp
40059c: f9000fe0 str x0, [sp, #24]
4005a0: f9400fe0 ldr x0, [sp, #24]
4005a4: 97ffffb7 bl 400480 <printf@plt>
4005a8: d503201f nop
4005ac: a8c27bfd ldp x29, x30, [sp], #32
4005b0: d65f03c0 ret
00000000004005b4 <main>:
4005b4: a9bf7bfd stp x29, x30, [sp, #-16]!
4005b8: 910003fd mov x29, sp
4005bc: 90000000 adrp x0, 400000 <_init-0x418>
4005c0: 911a4000 add x0, x0, #0x690
4005c4: 97fffff4 bl 400594 <message>
4005c8: 52800000 mov w0, #0x0 // #0
4005cc: a8c17bfd ldp x29, x30, [sp], #16
4005d0: d65f03c0 ret
4005d4: 00000000 .inst 0x00000000 ; undefined
6: Remove -O0 and add -O3 to the gcc options
$ gcc hello.c -o hello0 -g -O3 -fno-builtin && ll -h
-O3 optimized, including the inline function.
Contents of section .eh_frame_hdr:
Contents of section .debug_info:
Contents of section .debug_str:
Contents of section .debug_ranges:
0000 90044000 00000000 b0044000 00000000 ..@.......@.....
0010 00000000 00000000 00000000 00000000 ................
<main> part 1st position
Comments
Post a Comment