C# Program Compilation Steps

Introduction

In this article we are going to understand C# program compilation steps and contains of .EXE file.

Description

C# Program compilation steps are as follows.

  1. Compiling Source Code into Managed Module.
  2. Combining newly created managed module into the assembly / assemblies.
  3. Loading the CLR.
  4. Executing the assembly in & by CRL.

Every .NET program first compiles with an appropriate compiler like if we write a program in C# language then it get compiled by C# compiler (i.e. csc.exe).

In .NET framework every program executes (communicate) in an operating system by using CLR (Common Language Runtime).

clr

Compiling Source Code into Managed Module

  • All languages in .NET framework have their own compiler. Like C# has a C# compiler, JavaScript has a Jscript compiler, etc.

    Managed Module

  • Whenever source code gets compiled by its appropriate compiler it gets converted into a Managed Module.

  • Suppose if we create a console application & compile it then it creates .exe file. If we create web application & compile it then it creates .dll files.

  • Managed module is standard windows Portable Executable (PE) file which contains the following parts.

    • PE Header

      It is similar to common object file format.

    • CLR Header

      This contains CLR version required to run this managed module, location & metadata. This also contains entry point of function i.e. the address of entry point of function.

    • Metadata

      This contains table information means variable with its data types and default values, functions / methods which are declared & defined in our program.

      In symbol table all information is stored like the following code snippet:
      1. .int i = 25000;  
      2. string str = “Rupesh Kahane”;  
      .EXE file also contains Date Time, Owner name, on which operating system this .exe file is created with bit system like 32 bit or 64 bit. It also contains Magic Number which will get matched whenever we want to execute .exe file on our computer. If this magic number does not match with the operating system then it will generate error.

Combining newly created managed module into the assembly / assemblies

Assembly is nothing but .dll or .exe. In web application there are different program files which are written in different languages. This assembly combines & get together into a single assembly. It also contains images (i.e. resource files) & multiple managed modules.

Combining newly created managed module
(Diagram reference Wikipedia)

Loading the CLR

Loading the CLR

(Diagram reference Wikipedia)

Every program is located on hard disk. To run / execute each file it should have to come on RAM from hard disk means magnetic address space to electric address space. This is possible by using the help of the Loader.

Question: What is mscoree.dll in .Net framework?

Answer: (Reference Wikipedia).

  • It provides the possibility to connect information, systems, people and devices through software.
  • The mscoree.dll file is a Microsoft Runtime Execution Engine.
  • It contains the fundamental functions of the.NET framework.
  • Functions: quickly build, manage, deploy, & Security enhanced solutions with web services.

Question: What is _CoreExeMain function?

Answer: (Reference Wikipedia)

  • Initializes the CLR
  • Locates the managed entry point in the executable assembly’s CLR header, and begins execution.
  • This function is called by the loader in processes created from managed executable assemblies.
  • For DLL assemblies, the loader calls to _CoreDllMain function.

Executing the assembly in & by CLR

CLR means common language runtime. It is the heart of  the .NET framework which helps to execute the program on an operating system.

CLR provides some functionalities such as Garbage Collection, Code Verification, Memory Management, Code Access Security and IL to Native translation.

We can see PE Header, CLR Header, Metadata and Address Table values by dumpbin.exe:

  1. First install it in our local machine (download dumpbin.exe from internet).
  2. To see dumpbin.exe location in our local pc.
  3. This will be present in c drive where files of latest visual studio is installed, for example like the following:

    C:\Program Files<x86>\ Microsoft Visual Studio 12.0\VC\bin\amd64\dumpbin.exe

Use the following syntax:

C:\dumpbin /options /impots whatever.exe
A B C

Where,

A – location of dumpbin file
B – options for dumpbin file
C - location of our file (.exe or .dll)


Example:

C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64>dumpbin /all /imports D:\Demo.exe

Step 1: Create a simple console application & compile it. After compiling in bin folder we will get .exe file.

  1. class Program {  
  2.     public int a;  
  3.     public Program() {  
  4.         a = 25000;  
  5.     }  
  6.     public string str = "rupesh kahane";  
  7.     void fun() {  
  8.         Console.WriteLine(a);  
  9.         Console.WriteLine(str);  
  10.     }  
  11.     static void Main(string[] args) {  
  12.         Program obj = new Program();  
  13.         obj.fun();  
  14.         Console.ReadKey();  
  15.     }  
  16. }  
Step 2: Open command prompt. After opening the command prompt type cd.. to come out of current location of user account.

Open command prompt

Step 3: Please refer syntax now.

Firstly, open file location of dumpbin using cd command as

open file location

Step 4:
Now give options & import location of our .exe file (for the time being I have copied .exe file on F drive) as:

import location

Step 5: We will see the big screen which contains some sections as follows:

contains some sections

Relative Virtual Address (RVA)

It is a virtual address because file is present on a hard disk, so it will not show a direct address. It is the virtual address of an object from the file once it is loaded into the memory.

If the file were to be mapped literally from the disk to memory, the RVA would be the same as that of the offset into the file, but this is actually quite unusual.

Summary

This article helps fresher candidates to crack the technical interview rounds. Hope you enjoyed this one. Don’t forget to comment.

 

Up Next
    Ebook Download
    View all
    Learn
    View all