Introduce 介绍
.net 的中间语言MSIL(Microsoft Intermediate Language),
.net下生成的程序集(exe.dll)比较特殊,它的主要内容是MSIL,需要在.net环境下连接公共语言运行库(CLR)并由它代替编译成本地代码和执行本地代码.IL 不是字节代码或机器码,但与其非常接近.为了及时编译的速度
Explain&Use 解释&使用
ilasm
exe.dll 编译工具,位置: [系统盘]/WINDOWS/Microsoft.NET/Framework/v2.0.50727 ,如果要移动要包括 fusion.dll
查看元数据:Ctrl + M
命令行(command lines):
/exe 生成 .exe
/dll 生成 .dll
/output:<输出文件路径> 输出文件
/resource:<资源文件路径> 载入资源文件
http://msdn.microsoft.com/zh-cn/library/496e4ekx(VS.80).aspx
例子:ilasm.exe /exe /resource:test.res /output:test.exe test.il
ildasm
exe.dll反编译到IL及res资源文件的工具,位置 %ProgramFiles%/Microsoft SDKs/Windows/v6.0A/Bin
命令行(command lines):
/out=<输出文件路径> 输出文件
http://msdn.microsoft.com/zh-cn/library/f7dy01k1(v=vs.80).aspx
更多命令可输入 ildasm /help 查看
例子: ildasm /out=D:/test.il D:/test.exe
Glyph |
Text Output |
Description |
[MOD] for module heading |
Informational directives, class declarations, and manifest information |
|
[NSP] |
Namespace |
|
[CLS] |
Class |
|
[INT] |
Interface |
|
[ENU] |
Enumeration |
|
[VCL] |
Value class, also known as a structure |
|
[MET] |
Method (private, public, or protected) |
|
[STM] |
Static method |
|
[FLD] |
Field (private, public, or protected) also assembly |
|
[STF] |
Static field |
|
[EVT] |
Event |
|
[PTY] |
Property (get and/or set) |
Metadata(元数据)
描述数据特性的数据(data that describes data),当编译时产生。元数据描述代码中的类型,包括每种类型的定义、每种类型的成员的签名、代码引用的成员和运行库在执行时使用的其他数据。CLR根据这些信息,选择相应的方法处理代码.
更多解释:http://zh.wikipedia.org/wiki/%E5%85%83%E6%95%B0%E6%8D%AE
IL文件的结构
"Holle C sharp"的例子
- // Microsoft (R) .NET Framework IL Disassembler. Version 3.5.21022.8
- // Copyright (c) Microsoft Corporation. All rights reserved.
- /*定义配置类,组件,资源等*/
- // Metadata version: v2.0.50727
- .assembly extern mscorlib
- {
- .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z/V.4..
- .ver 2:0:0:0
- }
- .assembly 'hello c sharp'
- {
- .custom instance void [mscorlib]System.Reflection.AssemblyTitleAttribute::.ctor(string) = ( 01 00 0D 68 65 6C 6C 6F 20 63 20 73 68 61 72 70 // ...hello c sharp
- 00 00 )
- .custom instance void [mscorlib]System.Reflection.AssemblyDescriptionAttribute::.ctor(string) = ( 01 00 00 00 00 )
- .custom instance void [mscorlib]System.Reflection.AssemblyConfigurationAttribute::.ctor(string) = ( 01 00 00 00 00 )
- .custom instance void [mscorlib]System.Reflection.AssemblyCompanyAttribute::.ctor(string) = ( 01 00 09 4D 69 63 72 6F 73 6F 66 74 00 00 ) // ...Microsoft..
- .custom instance void [mscorlib]System.Reflection.AssemblyProductAttribute::.ctor(string) = ( 01 00 0D 68 65 6C 6C 6F 20 63 20 73 68 61 72 70 // ...hello c sharp
- 00 00 )
- .custom instance void [mscorlib]System.Reflection.AssemblyCopyrightAttribute::.ctor(string) = ( 01 00 1B 43 6F 70 79 72 69 67 68 74 20 C2 A9 20 // ...Copyright ..
- 4D 69 63 72 6F 73 6F 66 74 20 32 30 30 39 00 00 ) // Microsoft 2009..
- .custom instance void [mscorlib]System.Reflection.AssemblyTrademarkAttribute::.ctor(string) = ( 01 00 00 00 00 )
- .custom instance void [mscorlib]System.Runtime.InteropServices.ComVisibleAttribute::.ctor(bool) = ( 01 00 00 00 00 )
- .custom instance void [mscorlib]System.Runtime.InteropServices.GuidAttribute::.ctor(string) = ( 01 00 24 34 34 61 65 65 34 64 65 2D 36 37 35 37 // ..$44aee4de-6757
- 2D 34 65 61 64 2D 61 34 65 38 2D 61 37 32 35 31 // -4ead-a4e8-a7251
- 34 36 35 65 31 66 32 00 00 ) // 465e1f2..
- .custom instance void [mscorlib]System.Reflection.AssemblyFileVersionAttribute::.ctor(string) = ( 01 00 07 31 2E 30 2E 30 2E 30 00 00 ) // ...1.0.0.0..
- // --- 下列自定义属性会自动添加,不要取消注释 -------
- // .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 07 01 00 00 00 00 )
- .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 )
- .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx
- 63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows.
- .hash algorithm 0x00008004
- .ver 1:0:0:0
- }
- /*
- .module 'hello c sharp.exe'
- // MVID: {A7D63026-AD10-4250-801D-E90F8B18411F}
- .imagebase 0x00400000
- .file alignment 0x00000200
- .stackreserve 0x00100000
- .subsystem 0x0003 // WINDOWS_CUI
- .corflags 0x00000001 // ILONLY
- // Image base: 0x03470000
- // =============== CLASS MEMBERS DECLARATION ===================
- /*类列表*/
- .class private auto ansi beforefieldinit hello_c_sharp.Program
- extends [mscorlib]System.Object
- {
- .method private hidebysig static void Main(string[] args) cil managed
- {
- .entrypoint
- // 代码大小 19 (0x13)
- .maxstack 8
- IL_0000: nop
- IL_0001: ldstr "hello c charp"
- IL_0006: call void [mscorlib]System.Console::WriteLine(string)
- IL_000b: nop
- IL_000c: call string [mscorlib]System.Console::ReadLine()
- IL_0011: pop
- IL_0012: ret
- } // end of method Program::Main
- .method public hidebysig specialname rtspecialname
- instance void .ctor() cil managed
- {
- // 代码大小 7 (0x7)
- .maxstack 8
- IL_0000: ldarg.0
- IL_0001: call instance void [mscorlib]System.Object::.ctor()
- IL_0006: ret
- } // end of method Program::.ctor
- } // end of class hello_c_sharp.Program
- // =============================================================
IL常用命令解释
.entrypoint
定义函数入口点
.maxstack
声明函数代码所用堆栈的最大深度
ldstr <string>
把一个字符串常量装入堆栈
call <function(parameters)>
调用静态函数。函数的参数必须在函数调用前装入堆栈。
pop
取出栈顶的值。当我们不需要把值存入变量时使用。
ret
从一个函数中返回。
ldc.i4.<num>
把一个 32位的常量(n从0到8)装入堆栈
stloc.<num>
把一个从堆栈中返回的值存入第n(n从0到8)个局部变量
add
2个值相加。命令的参数必须在调用前装入堆栈,该函数从堆栈中移除参数并把运算后的结果压入堆栈。
sub
2个值相减。
mul
2个值相乘。
.locals <init ([0] int32 x)>
分配一个局部变量
例子: .locals init ([0] int32 x,[1] int32 y,[2] int32 z, [3] string s)
更多解释与例子:
http://www.cnblogs.com/Yahong111/archive/2007/08/15/857140.html ( MSIL 教程)
http://www.codeguru.com/Csharp/.NET/net_general/il/article.php/c4635 ( MSIL 教程英文版)
http://www.kuqin.com/dotnet/20090406/44365.html (认识元数据和IL)
http://www.dotblogs.com.tw/ajun/archive/2008/01/26/1011.aspx (玩玩.net的ildasm與ilasm )
http://bbs.pediy.com/showthread.php?threadid=16764 (ilasm 汇编小则)
http://hi.baidu.com/zhangshourui/blog/item/ed73a30fa72da9e9aa6457d3.html (通过MSIL了解CLR的运行原理)
http://www.cnblogs.com/qyjun/articles/736102.html (详细解释两个il例子)
附件:
http://download.csdn.net/source/3229951 (.net元数据指令术语解释说明.英文版)