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


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).

Question: What is _CoreExeMain function?

Answer: (Reference Wikipedia)

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.