The C compilation process | 0x02 | Bin Exp#3

0xCyberzombie xdev
4 min readApr 8, 2021

Hi friends, In my last blog I write about the introduction to C. Here we are going to break down the compilation process of C.So let’s get into it.

My setup

  1. GCC(C compiler)

2. Ubuntu 20.04

Compilation process

The process of changing the human-readable code to machine-understandable code is called compilation.

The C compilation process:

Preprocessing phase

Compilation phase

Assembly phase

Linking phase

The Preprocessing phase:

The process of expanding all the declined directives(#include) and macros(#define) to the source file is called preprocessing phase.

In our C code, we write #include and #define, here we only declining that in our code not defining. In the preprocessing phase, we only going to define all that type of directives and macros. let’s make it more understandable by seeing the example.

usually, the GCC compiler does all the four-step when we begin to compile a binary so we have to tell the GCC to stop compilation after the preprocessing end. This can be done by passing a -E and -P flag to GCC

Cmd :

$ gcc -E -P source.o -o source

  • -E → It tells the GCC to stop after preprocessing has done
  • -p → It tells the GCC to omit the debugging symbols so that the output became quite neat.
  • -o → It is to define a name of the binary that’s we want.

Our Source File:

Our Steps:

That’s it for the preprocessing phase.

The Compilation phase:

The process of converting all source code into Assembly code(X86–64)is called the compilation phase.

Here we going to convert the source code into assembly code(x86–64).To see the “behind the scenes” of this process we have to pass a flag to GCC that is -S and “-masm=intel” it well tell the GCC to stop the compilation process after the compilation phase has done

Cmd:

$ gcc -S -masm=intel source.c -o source

  • -S → It tells the GCC to stop after the Compilation phase.
  • -masm=intel → It tells the GCC to set the assembly code language to intel flavor.

That’s it for the Compilation phase.

The Assembly phase:

The process of converting the Assembly code into object files is called the Assembly phase.

Here we going to convert the assembly code into object files that are nothing but a binary file that is get merged by the linker in the linking phase. Typically each source file corresponds to one assembly file, and each assembly file corresponds to one object file and each object file corresponds to one executable file. This makes sense when we see about the ELF fundamental which is our next blog. You can break down the “behind the scenes” of this process by passing the -c flag to GCC.

Cmd:

$ gcc -c sourc.c -o source

-c → It told the GCC to stop the compilation after the assembly phase has done.

That’s it for the Assembly phase.

The Linking phase:

The process of merging a number of object files to create an executable is called the Linking phase.

Here we are going to merge the number of object files that are created in the assembly phase to form the final executable file. Here you not want to pass any flag to GCC because it is the final phase, and in general GCC will automatically do all 4 phases.

So that’s it guys we finished the C compilation process.

Hope you guys enjoy this blog and if you have any doubt comment to me.

And Don’t forget to subscribe to my YouTube channel.

--

--