from source code to executable : how executable is created during compilation on linux

We will use a self explanatory bash script, you can just copy paste the below highlighted code and create a file called
test_compilation.sh” and execute it on terminal as “bash test_compilation.sh”

This code is tested onĀ Ubuntu 15.04, Vivid. You may need to twick the script if you use a different version.

The steps for compilation as as below,

  1. write a source code helloworld.c
  2. run a preprocessor which create helloworld.i
  3. run a compiler which creates helloworld.s by taking preprocessed file helloworld.i as input.
  4. run a assembler which creates helloworld.o b taking helloworld.s as input
  5. run a linker which takes helloworld.o as input and generates helloworld executable

 

#!/bin/bash

echo “our helloworld program is”

echo “

#include <stdio.h>

#define MY_NAME \”DevBee\”

int main(int argc, char **argv) {
printf(\”validating preprocessor string: %s\n\”, MY_NAME);
printf(\”Hello world for understanding compilation\n\”);
return 0;
}

” > helloworld.c

echo “executing preprocessor”

/usr/lib/gcc/i686-linux-gnu/4.9/cc1 -E -quiet -v -imultiarch i386-linux-gnu helloworld.c -mtune=generic -march=i686 -fpch-preprocess -fstack-protector-strong -Wformat -Wformat-security -o helloworld.i

echo “executing compiler”
/usr/lib/gcc/i686-linux-gnu/4.9/cc1 -fpreprocessed helloworld.i -quiet -dumpbase helloworld.c -mtune=generic -march=i686 -auxbase helloworld -version -fstack-protector-strong -Wformat -Wformat-security -o helloworld.s

echo “executing assembler”
as -v –32 -o helloworld.o helloworld.s

echo “ececuting loader to create final executable”

/usr/lib/gcc/i686-linux-gnu/4.9/collect2 -plugin /usr/lib/gcc/i686-linux-gnu/4.9/liblto_plugin.so -plugin-opt=/usr/lib/gcc/i686-linux-gnu/4.9/lto-wrapper -plugin-opt=-fresolution=helloworld.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s –sysroot=/ –build-id –eh-frame-hdr -m elf_i386 –hash-style=gnu –as-needed -dynamic-linker /lib/ld-linux.so.2 -z relro -o helloworld /usr/lib/gcc/i686-linux-gnu/4.9/../../../i386-linux-gnu/crt1.o /usr/lib/gcc/i686-linux-gnu/4.9/../../../i386-linux-gnu/crti.o /usr/lib/gcc/i686-linux-gnu/4.9/crtbegin.o -L/usr/lib/gcc/i686-linux-gnu/4.9 -L/usr/lib/gcc/i686-linux-gnu/4.9/../../../i386-linux-gnu -L/usr/lib/gcc/i686-linux-gnu/4.9/../../../../lib -L/lib/i386-linux-gnu -L/lib/../lib -L/usr/lib/i386-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/i686-linux-gnu/4.9/../../.. helloworld.o -lgcc –as-needed -lgcc_s –no-as-needed -lc -lgcc –as-needed -lgcc_s –no-as-needed /usr/lib/gcc/i686-linux-gnu/4.9/crtend.o /usr/lib/gcc/i686-linux-gnu/4.9/../../../i386-linux-gnu/crtn.o

The files created during compilation time are as below,

$ file helloworld.c
helloworld.c: C source, ASCII text

 

$ file helloworld.i
helloworld.i: C source, ASCII text

SHUFFLED :   Kernel startup entry point / how Linux kernel boots

 

$ file helloworld.s
helloworld.s: assembler source, ASCII text

 

$ file helloworld.o
helloworld.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped

 

$ file helloworld
helloworld: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=e9c198a3e75c0d32d8c775381b3641cbcb8f8848, not stripped

 

You can referĀ http://www.tenouk.com/ModuleW.html for great details.

Android Android Commands Android Java Applications Application Libraries Bash / Shell Scripts Bluetooth driver Build Frameworks Commands and Packages Core Kernel C Programs Development Environment Setup Documents / Books Errors & Failures File Systems Framebuffer / Display Driver git Go Language Programs Hardware Platforms Home Kernel & Device Drivers Kernel Booting and Porting Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL NDK / Middleware / HAL Network Driver OS Concepts PHP Procfs Filesystem Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded
Tags:

Leave a Reply