How does the OS decide how to run a .exe

Darem :

Assuming I start a .exe file on Windows. This file could be written in C++, or in Java or C# and compiled to an .exe.

For C++ the operating system can execute the commands directly. But how does Windows know that it has to start the runtime (JVM or CLR) for Java or C#? And in addition how is decided which Runtime is started?

Michael Chourdakis :

For CLR, the info you are looking is at the PE header of the executable. Wiki and Microsoft Spec. There is a section in the sections list for CLR.

In a .NET executable, the PE code section contains a stub that invokes the CLR virtual machine startup entry, _CorExeMain or _CorDllMain in mscoree.dll, much like it was in Visual Basic executables. The virtual machine then makes use of .NET metadata present, the root of which, IMAGE_COR20_HEADER (also called "CLR header") is pointed to by IMAGE_DIRECTORY_ENTRY_COMHEADER[6] entry in the PE header's data directory. IMAGE_COR20_HEADER strongly resembles PE's optional header, essentially playing its role for the CLR loader.2

The CLR-related data, including the root structure itself, is typically contained in the common code section, .text. It is composed of a few directories: metadata, embedded resources, strong names and a few for native-code interoperability. Metadata directory is a set of tables that list all the distinct .NET entities in the assembly, including types, methods, fields, constants, events, as well as references between them and to other assemblies.

And from Microsoft:

The .cormeta Section (Object Only) CLR metadata is stored in this section. It is used to indicate that the object file contains managed code. The format of the metadata is not documented, but can be handed to the CLR interfaces for handling metadata.

For Java, there is a loader that loads the JAR embedded to the executable (pretty much like the old days of DOS loaders or EXE packers). This latter technique can be applied virtually to anything, e.g. a .bat file embedded inside an .EXE as a resource, which is loaded and executed by some loader. This does not need any help from the Windows loader.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=126090&siteId=1