Sunday, October 4, 2015

SPO600 Lab 2 - Examining compiler options

SPO600 - Lab 2

Modifying C program and compiling the program with gcc options and examining the output. Link to Lab is: SPO600_Compiled_C_Lab. Everything shown in this page has been ran on aarchie 64 machine.

Simple C - hello world program.

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

Compiling Using: gcc -g -O0 -fno-builtin hello.c
Examining Executable File Using:
objdump -f -s -d --source (executable filename)
Executable File Size: 9569 bytes

1. Add the compiler option -static. Note and explain the change in size, section headers, and the function call.
 
Executable File Size: 681707 bytes

Using the objdump command to determine changes gcc compiler did using -static option tell us that there was no changes made on how the main function is going to run, it just prevented linking with shared gcc libraries. This makes compiler copy all required information from gcc libraries to the executable file.

2. Remove the compiler option -fno-builtin. Note and explain the change in the function call.


Executable File Size: 9182 bytes

One small changed has been made in executable file, In original executable the printf() function is called to print
the line/it makes a one line function results in smaller and faster code but without -fno-builtin option the printf()
is using puts@plt option it puts printf() function on stack so it will be called later.

3. Remove the compiler option -g. Note and explain the change in size, section headers, and disassembly output.
Executable File Size: 8192 bytes

The executable file doesn't contain information on what each line does in main function, -g option adds debuggig information
to the executable file.

4. Add additional arguments to the printf() function in your program. Note which register each argument is placed in.


Executable File Size: 9456 bytes

Modified print line: printf("Helo World! %d, %d, %d, %d, %s, %f, %f, %f, %f, %s\n", 10, 20, 30, 40, "fifty", 59.99, 69.99, 79.99, 89.99, "Hundred");
I added multiple intergers, floats and strings arguments to printf funtion and the compiler uses ldr to create floating variables and fmove
pushes stores these variable into memory and adrp makes a string variables and add pushes/adds these varibles to memory.

5. Move the printf() call to a separate function named output(), and call that function from main(). Explain the changes in the object code.

Executable File Size: 9359 bytes

The executable file has few steps which is doing the same thing in both function like: stp and mov steps after function calls and
ldp and ret after function closing. Two bl (callq) are being made one to output function from main and then another to printf function
from output function these extras and repeting steps are made, for this scenario the function calls are too many then actucal task(printf) thats why this is inefficient. Having an extra function adds more work to the program and it provides less performance.

6. Remove -O0 and add -O3 to the gcc options. Note and explain the difference in the compiled code.

Executable File Size: 10616 bytes

The main function doesn't have the steps stp, mov, ldp and ret for main function because in main function is calling printf function and nothing else so the compiler doesn't save information about main function. Before it use to make callq to printf function and then comes back to main functions to finish its steps but now it just jumps to printf function and doesn't come back to make closing statements on main like ldp and ret because there isn't anything other then printf function so the return from printf function will go to operating system rather then going back to main function and main function returning to operating system. Option -O3 is optimized for program speed over memory space, the compiler will apply all optimiations to make it's speed better without worrying about memory space.

No comments:

Post a Comment