Tuesday, November 17, 2020

Explain what happens in the background when a source code in C Sharp is compiled and finally an executable code is generated?

 

C# Program Compilation Steps




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
  1. 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 a .exe file. If we create a web application & compile it then it creates .dll files.

  • The managed module is standard windows Portable Executable (PE) file that contains the following parts.
  • PE Header

    It is similar to the common object file format.

  • CLR Header

    This contains the CLR version required to run this managed module, location & metadata. This also contains the entry point of the function i.e. the address of the entry point of the 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 the 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 systems like 32 bit or 64 bit. It also contains Magic Number which will get matched whenever we want to execute the .exe file on our computer. If this magic number does not match with the operating system then it will generate an error.

2. Combining a 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)

4.Loading the CLR

Loading the CLR

(Diagram reference Wikipedia)

Every program is located on a 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 the .Net framework?
  • 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 the _CoreExeMain function?
  • 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.

4.Executing the assembly in & by CLR

CLR means a 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 on our local machine (download dumpbin.exe from the internet).
  2. To see dumpbin.exe location in our local pc.




No comments:

Post a Comment

NET Core Code Security, Authorization/Authentication, and code architecture

Complex NET Core interview questions (Code Security, Authorization/Authentication, and code architecture)  _________________________________...