Why is an assembly .exe file?

2019-01-13 14:09发布

问题:

Assembly in .net Framework is, as I understand, intermediate language file + some metadata, manifest and maybe something else.

CLR translates an assembly to the machine code, which can be executed on the given local machine.

That means that assembly shouldn't be executable by the machine before being processed by CLR. If it's so, then why does it have .exe extension, which is executable on Windows machines?

回答1:

Since Windows needs to create a process and the first thing .exe will do is to host CLR by loading mscoree.

From CLR via C#:

After Windows has examined the EXE file's header to determine whether to create a 32-bit process, a 64-bit process, or a WoW64 process, Windows loads the x86, x64, or IA64 version of MSCorEE.dll into the process's address space. On an x86 version of Windows, the x86 version of MSCorEE.dll can be found in the C:\Windows\System32 directory. On an x64 or IA64 version of Windows, the x86 version of MSCorEE.dll can be found in the C:\Windows\ SysWow64 directory, whereas the 64-bit version (x64 or IA64) can be found in the C:\Windows\System32 directory (for backward compatibility reasons). Then, the process' primary thread calls a method defined inside MSCorEE.dll. This method initializes the CLR, loads the EXE assembly, and then calls its entry point method (Main). At this point, the managed application is up and running.



回答2:

I have recently written a blog post on the CLR stub in a .NET assembly and how it fits inside the PE file executable format. The whole series can be found here.

Essentially, inside a .NET assembly is a tiny bit of native code that starts the CLR. However, this code only exists for backwards-compatibility. From Windows XP, the OS loader natively knows to load the CLR for executable that have the CLI header in them.



回答3:

A .NET .exe is a special kind of .exe that will first load up the CLR (Common Language Runtime). It will then compile the IL code inside the .exe through the CLR.

An .exe compiled through the .NET framework is a kind of Portable Executable. The .exe has a CLR Header and CLR Data section. When the .exe loads, the OS will yield control to the CLR. The CLR Data section in the .exe has a metadata and an IL (Intermediate Language) segment. The metadata segment contains information about the assembly (such as the assembly manifest). The IL segment contains the code for the program in IL format. This is an intermediate format similar to Java bytecode.



回答4:

Since it's still an EXEcutable.



标签: .net clr