DOTNET Assembly more basic understanding [anycpu 32bit/64bit assembly/executable/CLR]

Source: Internet
Author: User
Tags dotnet

1. anycpu

Firstly, there are 32bit/64bit CLR. for executables, it's up to the CPU bit to decide whether it will load as a 32/64 bit process with corresponding CLR; for assembly, it's up to the process it's to be loaded into not the CPU or OS bit.

/Platform (specify output platform) (C # compiler options)

Remarks

  1. X86 compiles your assembly to be run by the 32-bit, x86-compatible common language runtime.

  2. Itanium compiles your assembly to be run by the 64-bit Common Language Runtime on a computer with an itanium processor.

  3. X64 compiles your assembly to be run by the 64-bit Common Language Runtime on a computer that supports the amd64 or em64t instruction set.

  4. Anycpu (default) compiles your assembly to run on any platform.

On a 64-bit Windows operating system:

  1. Assemblies compiled/Platform: x86Will execute on the 32 bit CLR running under wow64.

  2. Executables compiled with/Platform: anycpuWill execute on the 64 bit CLR.

  3. DLLs compiled with/Platform: anycpuWill execute on the same CLR as the process into which it is being loaded.

A greate example:

 

Let's assume we have the following three DLLs:

Anycpu. dll -- compiled "any CPU"

X86.dll -- compiled "x86"

X64.dll -- compiled "x64"

 

And the following three exes:

Anycpu.exe -- compiled "any CPU"

X86.exe -- compiled "x86"

X64.exe -- compiled "x64"

 

What happens if you try to use these exes and DLLs together? We have to consider two possible scenarios, running on a 32-bit machine and running on a 64-bit machine...

 

On a 32-bit x86 machine:

Anycpu.exe -- runs as a 32-bit process, can load anycpu. dll and x86.dll, will get badimageformatexception if it tries to load x64.dll

X86.exe -- runs as a 32-bit process, can load anycpu. dll and x86.dll, will get badimageformatexception if it tries to load x64.dll

X64.exe -- will get badimageformatexception when it tries to run

 

On a 64-bit x64 machine:

Anycpu.exe -- runs as a 64-bit process, can load anycpu. dll and x64.dll, will get badimageformatexception if it tries to load x86.dll

X86.exe -- runs as a 32-bit process, can load anycpu. dll and x86.dll, will get badimageformatexception if it tries to load x64.dll

X64.exe -- runs as a 64-bit process, can load anycpu. dll and x64.dll, will get badimageformatexception if it tries to load x86.dll

2. DOTNET Assembly x32bit/x64bit

A dotnet assembly can be compiler targeting anycpu, x32 and x64. strictly, if it contains only managed code without native code, then it contains only Il. it's really platform agnostic. but in order to let JIT decide the final platform for compiling, there is a header section in DOTNET assembly to tell platform info but doesn't contain specific platform code at all:

Corflags.exe is a tool that is shipped as a part of.. NET Framework SDK. it is a powerful tool which allows users to view and configure the corflags section of a PE image. all the data related to platforms is available in this part of the PE image.

 

A look at an example of a PE Header seen from this tool for a 32-bit assembly is below

 

Version: v2.0.50727

CLR head: 2.5

PE: pe32

Corflags: 11

Ilonly: 1

32bit: 1

Signed: 1

 

Here is what each component of the header means

Version: Contains the version of. Net redist with which the binary is built.

CLR Header: 2.0 indicates a. NET 1.0 or. NET 1.1 (Everett) image while 2.5 indicates a. NET 2.0 (Whidbey) image.

Corflags: This is computed by or 'G specific flags to indicate whether the image is ilonly, its bitness etc. and is used by the loader.

Ilonly: Managed images are allowed to contain native code. To be "anycpu" an image shall only contain Il.

32bit: Even if you have an image that only contains Il it still might have platform dependencies, The 32bit flag is used to distinguish "x86" images from "anycpu" images. 64-bit images are distinguished by the fact that they have a PE type of pe32 +.

 

The most interesting aspect is the PE and the 32bit flag of the header. These combine to specify the Assembly types. Here is how they wowould look like:

· Anycpu: PE = pe32 and 32bit = 0

· X86: PE = pe32 and 32bit = 1

· 64-bit: PE = pe32 + and 32bit = 0

 

 

References:

1. http://blogs.msdn.com/ B /rmbyers/archive/2009/06/08/anycpu-exes-are-usually-more-trouble-then-they-re-worth.aspx

2. http://blogs.msdn.com/ B /gauravseth/archive/2006/03/07/545104.aspx

3. http://efreedom.com/Question/1-3300877/Best-Way-Support-Multiple-Architectures-Mixed-Managed-Unmanaged-Environment

4. http://blogs.msdn.com/ B /joshwil/archive/2005/04/08/406567.aspx

5. http://blogs.msdn.com/ B /dsvc/archive/2008/06/28/x86-x64-ia64-any-cpu.aspx

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.