Thursday, 14 February 2013

Let's Take Example Of C Language..It has 4 Stages.. 1. Preprocessing:(In gcc, it is the 'gcc -E' stage).Here, The Preprocessor replaces the #include files, include guards, #define (macros), etc with code.

2. Compiling:(In gcc, It is the 'gcc -c' stage) Turns code (after preprocessor is done with it) from human understandable text to "machine code".Basically, into assembly language (creates .o files for instance).To be noted here is that the .o files so created still have textual symbols in them, not actual memory addresses.

3. Linking:(in gcc,It is gcc -o stage) Turns the files produced from Compilation stage (object files like .o, .so files, etc) into actual executables (a.out, or a.exe).This is where two things happen: A. Relocation: The Executable needs to be "built" into a format that the OS will understand. Whatever the compiler hands off to the linker needs to be laid out into the executable's data segment (holds program data), text segment (holds program code), heap (holds dynamically allocated memory through malloc, calloc, etc), stack (holds program stacks - return addresses for functions called), etc.

B. Symbol resolution: The symbols generated from step 2, are actually resolved into addresses here. These are not actual physical addresses in memory; these are some "temp" addresses. Resolving them into actual addresses in memory is a job that is done by the operating system's memory manager.