Www.helper3.com
热门关键字: php net javascript java
博学而笃志 切问而近思 仁在其中
详情
CLR运行机制
作者:Aliot     发布时间:2020-08-03 06:04:18     阅读:10

CLR, which is Microsoft’s implementation of the virtual execution

environment, targeting .NET languages(译:CLR是微软.net语言的虚拟执行环境的一种实现)

A virtual machine, such as Microsoft’s CLR, uses the JIT compiler to generate optimized native machine code from the intermediate code at runtime; manages and controls the execution of the application, using the method state; manages the object life cycle, using the GC; and so on.(注:这里CLR的概念被描述为一种“虚拟机”,后面介绍时说是一种window进程的表现形式)

CLR: Virtual Machine for .NET

The CLR is the Microsoft implementation of the virtual execution environment. The CLR manages the execution of source code written using C#, VB.NET, or any other language supported by .NET. The source code is first compiled into MSIL, and later, during the execution phase, it is compiled into native code.

The CLR offers many services, such as code management; software memory isolation; loading and execution of managed assembly; and compilation of the IL code into native code, including verification of the type safety of the MSIL code. The CLR also accesses the metadata embedded within the assembly to lay out the type information in

memory and provides memory management, using the GC. In addition, the CLR handles exceptions, including cross-language exceptions.

CLR is a Windows process, and it controls the execution of the managed code.The CLR itself cannot be loaded into memory to work as the execution environment; it needs to be triggered to load and start as a process. In this case, a piece of code, known as bootstrapper, which is embedded in the application program during compilation, is used to trigger the CLR to initialize and load into memory. Once the CLR has been loaded, it is in charge of the application program for which it’s being loaded, and it will take care of execution of that

application program.(译:CLR是一个window进程,它控制托管代码的执行。CLR不能单独运行。它需要被(第三方)触发加载并以进程的方式启动。在这种情况下,一段名叫bootstrapper的代码在编译期间会被自动嵌入到应用程序中,用于触发CLR的初始化和载入内存。一旦CLR被加载,就全面接管加载的应用程序,并负责应用程序的执行)

CLR Bootstrapping

The CLR is made up of a number of DLLs. It does not do anything unless they are being loaded by an application as a host for them. The CLR works as the mediator between the application program and the OS. To load these DLLs to initialize the execution environment and start executing the application program, the application program needs to tell the Windows loader that this is a .NET application and that the CLR has to be loaded.(译:CLR由一组DLL组成。在应用程序载入这些DLL之前它无法做任何事情。CLR工作在应用程序和操作系统的中间。为了加载这些DLL以初始化net的执行环境并执行应用程序,应用程序需要告诉Window加载器:这是一个.NET应用程序,CLR必须要加载)

The bootstrapping process of the CLR is triggered by a piece of code. This thin piece of code, embedded in the assembly, triggers the execution process of the application program. Figure 6-4 gives a high-level overview of the CLR bootstrapping process.(译:CLR的bootstrapping的启动由一段代码触发,这段代码嵌入在程序集里,负责触发应用程序的执行进程。图6-4展示了CLR bootstrapping的执行过程:操作系统加载器被应用程序通知去调用mscoree.dll的_CorExeMain方法,_CorExeMain则调用Shim(a block of code,only one version of the shim exists on a given machine, and that version is installed on the machine’s default search path (currently %windir%system32))的_CorExeMain方法完成CLR的加载)。

Figure 6-4. CLR bootstrapping


 

During the shim process the shim will call the mscoreei!_CorExeMain method to ascertain the entry point of the assembly for starting the execution. Typically, the entry point is the Main method for the executable assembly。(译:在shim进程中,shim的_CorExeMain方法会确认程序集的入口点(entry point)的位置并开始执行应用程序。通常可执行程序集的入口点就是Main方法)。个人理解:要开始执行应用程序的前提是完成了CLR的加载,所以这里的顺序应该是先完成CLR的加载(此时CLR将接管控制权),准备好net的执行环境,然后CLR开始读取程序集里面的程序清单、元数据、IL代码,并从程序集的入口点方法开始执行。注意,是CLR执行IL代码而不是cpu处理器直接处理,IL代码这个时候相当于CLR将要处理的数据而已。



Window执行机制

Registers have a variety of purposes, such as holding the addresses of instructions and data, storing the result of an operation, signaling the result of a logic operation, and indicating the status of the program or the CPU itself. Some registers may be accessible to programmers, whereas others are reserved for use by the CPU. Registers store binary values (1s and 0s) as electrical voltages, such as 5 volts or less. Registers consist of several integrated transistors, which are configured as flip-flop circuits, each of which can be switched to a 1 or 0 state. Registers remain in that state until changed by the CPU or until the processor loses power.

Each register has a specific name and address. Some are dedicated to specific tasks, but the majority are general purpose. The width of a register depends on the type of CPU (16 bit, 32 bit, 64 bit, and so on).

REGISTERS

• General purpose registers : Registers (eight in this category) for storing operands and pointers

• EAX: Accumulator for operands and results data

• EBX: Pointer to data in the data segment (DS)

• ECX: Counter for string and loop operations

• EDX: I/O pointer

• ESI: Pointer to data in the segment pointed to by the DS register; source pointer for string

operations

• EDI: Pointer to data (or destination) in the segment pointed to by the ES register; destination

pointer for string operations

• ESP : Stack pointer (in the SS segment)

• EBP : Pointer to data on the stack (in the SS segment)

• Segment registers : Hold up to six segment selectors.

• EFLAGS (program status and control) register : Reports on the status of the program being

executed and allows limited (application-program level) control of the processor

• EIP (instruction pointer) register : Contains a 32-bit pointer to the next instruction to be

Executed

The segment registers (CS, DS, SS, ES, FS, GS) hold 16-bit segment selectors. A segment selector is a special pointer that identifies a segment in memory. To access a particular segment in memory, the segment selector for that segment must be present in the appropriate segment register. Each of the segment registers is associated with one of three types of storage: code, data, or stack. For example, the CS register contains the segment selector for the code

segment, where the instructions being executed are stored.

The DS, ES, FS, and GS registers point to four data segments. The availability of four data segments permits efficient and secure access to different types of data structures. For instance, four separate data segments may be created—one for the data structures of the current module, another for the data exported from a higher-level module, a third for a dynamically created data structure and a fourth for data shared with another program. The SS register contains the segment selector for the stack segment, where the procedure stack is stored for the program, task, or handler currently being executed. All stack operations use the SS register to find the stack segment.

Unlike the CS register, the SS register can be loaded explicitly, which permits application programs to set up multiple stacks and switch among them.

The CPU will use these registers while executing any program, and the OS maintains the state of the registers while executing multiple applications by the CPU.

 



相关文章
loading......
滚动到顶部 滚动到底部
网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
QQ群:570078429
网站备案号:
粤ICP备17073857号