Compiling Source Code into Managed Modules
Decide which programming language to use
- Microsoft has created several language compilers that target the runtime: C++/CLI, C# , Visual Basic, F# , ... and an Intermediate Language (IL) Assembler .
- In fact, at runtime, the CLR has no idea which programming language the developer used for the source code .
- Different programming languages allow you to develop using different syntax .Don’t underestimate the value of this choice .
Compiling source code into managed modules
A managed module is a standard 32-bit Microsoft Windows portable executable (PE32) file or a standard 64-bit Windows portable executable (PE32+) file that requires the CLR to execute .
Parts of a Managed Module :
| Part | Description |
| PE32 or PE32+ header | The standard Windows PE file header. |
| CLR header | Includes the version of the CLR required, some flags, the MethodDef metadata token of the managed module’s entry point method (Main method), and the location/size of the module’s metadata, resources, strong name, some flags, and other less interesting stuff . |
| Metadata | Describe the types and members defined in your source code and the types and members |
| IL code | Code the compiler produced as it compiled the source code . At runtime, the CLR compiles the IL into native CPU instructions . |
Uses of Metadata
- Metadata removes the need for native C/C++ header and library files when compiling .
- IntelliSense.
- The CLR’s code verification process uses metadata to ensure that your code performs only “type-safe” operations .
- Serialization .
- Metadata allows the garbage collector to track the lifetime of objects .
Combining Managed Modules into Assemblies
(_A~B%7DBN%60O_3.jpg)
The CLR doesn’t actually work with modules, it works with assemblies . First, an assembly is a logical grouping of one or more modules or resource files . Second, an assembly is the smallest unit of reuse, security, and versioning . An assembly’s modules also include information about referenced assemblies .
The manifest is simply another set of metadata tables . These tables describe the files that make up the assembly, the publicly exported types implemented by the files in the assembly, and the resource or data files that are associated with the assembly .
Loading the Common Language Runtime
.NET Framework must be installed on the host machine
- You can tell if the .NET Framework has been installed by looking for the MSCorEE .dll file in the %SystemRoot%\System32 directory.
- Several versions of the .NET Framework can be installed on a single machine simultaneously.
- Determine exactly which versions are installed:CLRVer.exe or HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP .
32-bit and 64-bit versions of Windows
If your assembly files contain only type-safe managed code, you are writing code that should work on both 32-bit and 64-bit versions of Windows .
To write code that works only on a specific version of Windows Use the /platform command-line switch .
00
Loading the Common Language Runtime
- Windows examines this EXE file’s header to determine whether the application requires a 32-bit or 64-bit address space .
- Windows loads the x86, x64, or IA64 version of MSCorEE .dll into the process’s address space.
- The process’s 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) .
Executing Your Assembly’s Code
A performance hit is incurred only the first time a method is called . All subsequent calls to the method execute at the full speed of the native code because verification and compilation to native code don’t need to be performed again .
Managed code can outperform unmanaged code:
- A JIT compiler can use special instructions that would give the application a performance boost .
- A JIT compiler can determine when a certain test is always false on the machine that it is running on . the resulting code may be smaller and executes faster .
- The CLR could profile the code’s execution and recompile the IL into native code while the application runs .The recompiled code could be reorganized to reduce incorrect branch predictions depending on the observed execution patterns .
IL and Verification
IL is stack-based.IL instructions are also typeless.
The biggest benefit IL provides is application robustness and security.While compiling IL into native CPU instructions, the CLR performs a process called verification . Verification examines the high-level IL code and ensures that everything the code does is safe . The managed module’s metadata includes all of the method and type information used by the verification process .Verification checks:
- every method is called with the correct number of parameters.
- each parameter passed to every method is of the correct type.
- every method’s return value is used properly.
- every method has a return statement, and so on .
- and so on
The CLR does, in fact, offer the ability to execute multiple managed applications in a single OS process . Each managed application executes in an AppDomain .
Unsafe Code
- Unsafe code is allowed to work directly with memory addresses and can manipulate bytes at these addresses . /unsafe compiler switch .
- When the JIT compiler attempts to compile an unsafe method, it checks to see if the assembly containing the method has the System.Security.Permissions.SecurityPermissionFlag’s SkipVerification flag set .
- PEVerify .exe, which examines all of an assembly’s methods and notifies you of any methods that contain unsafe code .
The Native Code generator Tool: Ngen.exe
The NGen .exe tool can be used to compile IL code to native code when an application is installed on a user’s machine . This NGen’d file is placed in a folder under the directory with a name like C:\Windows\Assembly\NativeImages_v4 .0 .#####_64 , whenever the CLR loads an assembly file, the CLR looks to see if a corresponding NGen’d native file exists .
- Improving an application’s startup time .
- Reducing an application’s working set. the NGen’d file can be memory-mapped into multiple-process address spaces simultaneously, allowing the code to be shared.
Problems:
- No intellectual property protection.The CLR requires access to the assembly’s metadata ; this requires that the assemblies that contain IL and metadata be shipped .
- NGen’d files can get out of sync .When the CLR loads an NGen’d file, it compares a number of characteristics about the previously compiled code and the current execution environment . List of characteristics that must match:
- CLR version: this changes with patches or service packs
- CPU type: this changes if you upgrade your processor hardware
- Windows OS version: this changes with a new service pack update
- Assembly’s identity module version ID (MVID): this changes when recompiling
- Referenced assembly’s version IDs: this changes when you recompile a referenced assembly
- Security: this changes when you revoke permissions (such as declarative inheri-tance, declarative link-time, SkipVerification, or UnmanagedCode permissions),that were once granted
- Inferior execution-time performance.When compiling code, NGen can’t make as many assumptions about the execution environment as the JIT compiler can .
The Framework Class Library
The .NET Framework includes the Framework Class Library (FCL).
- Web services
- Web Forms HTML-based applications (Web sites)
- Rich Windows GUI applications
- Rich Internet Applications (RIAs)
- Windows console applications
- Windows services
- Database stored procedures
- Component library
The Common Type System
Microsoft created a formal specification—the Common Type System (CTS)—that describes how types are defined and how they behave .
- Field
- Method
- Property
- Event
The CTS also specifies the rules for type visibility and access to the members of a type .
- Private
- Family(protected)
- Family and assembly: The member is accessible by derived types, but only if the derived type is defined in the same assembly .
- Assembly(internal)
- Family or assembly (protected internal): The member is accessible by derived types in any assembly . The member is also accessible by any types in the same assembly
- Public
In addition, the CTS defines the rules governing type inheritance, virtual methods, object lifetime, and so on .
The Common Language Specification
The CLR now integrates all languages and allows objects created in one language to be treated as equal citizens by code written in a completely different language .Microsoft has defined a Common Language Specification (CLS) that details for compiler vendors the minimum set of features their compilers must support.
Interoperability with unmanaged Code
- Managed code can call an unmanaged function in a DLL. P/Invoke
- Managed code can use an existing COM component (server). TlbImp .exe
- Unmanaged code can use a managed type (server). TlbExp .exe and RegAsm .exe
浙公网安备 33010602011771号