最近在看Enpert .NET 2.0 IL Assembler这本书,可能会花去我很多时间,不过只是看了第一张就被它折服了,原作者简直是庖丁解牛,层层深入。翻译又是那么牛B,此书建议有想深入了解NET的朋友一看。
下面用vs自带的IL汇编和反汇编器做个小demo,算是对工具的简单使用吧。
vs建个控制台程序,fm 4.0的。来个hello world。然后编译下生成下,去它的bin目录找到exe
打开ILDASM工具。它在这里:C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\ildasm.exe
打开它,然后将刚刚生成的exe拖进去,然后【文件-->转存】为IL文件。这里关于ildasm工具的各种图标意义可以看它的帮助文档(chm),这里贴上
可以用文本编辑器打开这个il文件,结构如下:
// Microsoft (R) .NET Framework IL Disassembler. Version 4.0.30319.33440 // Metadata version: v4.0.30319 .assembly extern mscorlib { .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4.. .ver 4:0:0:0 } .assembly helloworld { .custom instance void [mscorlib]System.Runtime.Versioning.TargetFrameworkAttribute::.ctor(string) = ( 01 00 1A 2E 4E 45 54 46 72 61 6D 65 77 6F 72 6B // ....NETFramework 2C 56 65 72 73 69 6F 6E 3D 76 34 2E 30 01 00 54 // ,Version=v4.0..T 0E 14 46 72 61 6D 65 77 6F 72 6B 44 69 73 70 6C // ..FrameworkDispl 61 79 4E 61 6D 65 10 2E 4E 45 54 20 46 72 61 6D // ayName..NET Fram 65 77 6F 72 6B 20 34 ) // ework 4 .custom instance void [mscorlib]System.Reflection.AssemblyTitleAttribute::.ctor(string) = ( 01 00 0A 68 65 6C 6C 6F 77 6F 72 6C 64 00 00 ) // ...helloworld.. .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 00 00 00 ) .custom instance void [mscorlib]System.Reflection.AssemblyProductAttribute::.ctor(string) = ( 01 00 0A 68 65 6C 6C 6F 77 6F 72 6C 64 00 00 ) // ...helloworld.. .custom instance void [mscorlib]System.Reflection.AssemblyCopyrightAttribute::.ctor(string) = ( 01 00 12 43 6F 70 79 72 69 67 68 74 20 C2 A9 20 // ...Copyright .. 20 32 30 31 35 00 00 ) // 2015.. .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 36 36 31 66 62 31 61 66 2D 62 30 36 36 // ..$661fb1af-b066 2D 34 32 63 38 2D 38 33 61 37 2D 61 38 35 34 31 // -42c8-83a7-a8541 34 32 62 36 39 36 34 00 00 ) // 42b6964.. .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 helloworld.exe // MVID: {35A069E0-CDD1-4B8A-B64F-AFE8AD9880B1} .imagebase 0x00400000 .file alignment 0x00000200 .stackreserve 0x00100000 .subsystem 0x0003 // WINDOWS_CUI .corflags 0x00000001 // ILONLY // Image base: 0x008A0000 // =============== CLASS MEMBERS DECLARATION =================== .class private auto ansi beforefieldinit helloworld.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 "Helo World!" IL_0006: call void [mscorlib]System.Console::WriteLine(string) IL_000b: nop IL_000c: call valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey() 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 helloworld.Program // ============================================================= // *********** 反汇编完成 *********************** // 警告: 创建了 Win32 资源文件 C:\Users\Jon\Desktop\test.res
它的位置是:C:\Windows\Microsoft.NET\Framework\v4.0.30319\ilasm.exe
2.0对应的是 C:\Windows\Microsoft.NET\Framework\v2.0.50727\ilasm.exe
然后用ilasm命令编译il并输出到目录下:
cmd控制台打开,定位到ilasm目录下,运行命令(注:我用ildasm反编译exe之后的il文件名改成了test.il,所以下面的名字中是test.il)
C:\Windows\Microsoft.NET\Framework\v4.0.30319>ilasm.exe c:\test.il /output=f:\test.exe /exe意思是讲c盘根目录下的test.il文件编译输出到f盘下的test.exe文件。
如果控制台出现Operation completed successfully表示编译操作成功。
这只是对工具的简单使用,重点是读懂msil,以及对msil的随心所欲的修改。路漫漫其修远兮,吾将上下而求索。