Compiling C on AARCHIE

c programming banner

 

The purpose of this lab is to investigate basic C source code and analyze elf64-littleaarch64 files created by the gcc compiler.

To demonstrate the differences I created a simple hello world program in C and compiled it on an Seneca’s AARCH64 system “aarchie”.

//hello.c
#include <stdio.h>

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

// -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 optimizations

//compile command
gcc -g -o0 -fno-builtin -o hello-0 hello.c

Running the Test

Follow the instructions/commands bellow to run this test on your own system:

  1. Download and unzip spo600_mabeltempo_lab2.zip
  2. Change to the “lab2” directory
    cd lab2
    
  3. Compiles “hello-0.c” – “hello-6.c” using different compile flags for each.
    make
    
  4. Executes all the compiled hello-# programs, and export the results of  “objdump” to a text file.
    make run
    

 

Results

For each test, I’ve created a link to a file diff page for each objdump that will highlight the changes made. Each test was compared to the original hello.c file, using the compiler noted above.

(1) Add the compiler option -static.

hello-0_objdump.txt <-> hello-1_objdump.txt

//-static option links a program statically, and does not require a dynamic libraries at run time
//compile command
$gcc -g -O0 -fno-builtin -static -o hello-1 hello.c

The programs uses the static version of the C library opposed to dynamic libraries.
File Size
hello-0:72kb
hello-1: 815kb

(2) Removed the compiler option -fno-builtin.

hello-0_objdump.txt <-> hello-2_objdump.txt

//compile command
$gcc -g -O0 -o hello-2 hello.c

(3) Removed the compiler option -g. Note and explain the change in size, section headers, and disassembly output.

hello-0_objdump.txt <-> hello-3_objdump.txt

//compile command
$gcc -O0 -fno-builtin -o hello-3 hello.c

hello-0:72kb
hello-3:70kb
The debug header was completely removed resulting in a smaller file size.

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

hello-0_objdump.txt <-> hello-4_objdump.txt

//hello-4.c
#include <stdio.h>

int main() {
printf(“Hello %s%c %d \n”,”world”,’!’, 2017 );
}

//compile command
$gcc -g -O0 -fno-builtin -o hello-4 hello-4.c

(5) A separate function named output() was created with printf(). call to a separate function named output(), and call that function from main().

hello-0_objdump.txt <-> hello-5_objdump.txt

//hello-5.c
#include <stdio.h>

void output(){
printf(“Hello World!\n”);
}
int main() {
output();
}

//compile command
$gcc -g -O0 -fno-builtin -o hello-5 hello-5.c

(6) Removed -O0 and added -O3 to the gcc options. Note and explain the difference in the compiled code.

hello-0_objdump.txt <-> hello-6_objdump.txt

//compile command
$gcc -g -O3 -fno-builtin -o hello-6 hello.c

Leave a Reply

Categories

Follow me on Twitter