先來解釋下文要提到的幾個名詞:
PE文件:可移植可執行文件。當為公共語言運行庫編譯程序時,該程序轉換為由三部分組成的 PE 文件,PE 標頭、MSIL 指令、元數據。
MSIL: Microsoft 中間語言。這是一組可以有效地轉換為本機代碼且獨立于 CPU 的指令。MSIL 包括用于加載、存儲和初始化對象以及對對象調用方法的指令,還包括用于算術和邏輯運算、控制流、直接內存訪問、異常處理和其他操作的指令。
MSIL 反匯編程序是 MSIL 匯編程序 (Ilasm.exe) 的伙伴工具。Ildasm.exe 采用包含 Microsoft 中間語言 (MSIL) 代碼的可移植可執行 (PE) 文件,并創建相應的文本文件作為 Ilasm.exe 的輸入。
還是以SampleClass為例來解釋它們的用法。
MSIL反匯編程序(Ildasm.exe)
下面的命令使 PE 文件 SampleClass.exe 的元數據和反匯編代碼顯示在 Ildasm.exe 的默認 GUI 中。
ildasm SampleClass.exe
下面的命令對 SampleClass.exe 文件進行反匯編,并將結果 MSIL 匯編程程序文本存儲在 SampleClass.il 文件中。
E:/test>ildasm SampleClass.exe /output:SampleClass.il
// WARNING: Created Win32 resource file SampleClass.res
下面的命令對 SampleClass.exe 文件進行反匯編,并將結果 MSIL 匯編程序文本顯示到控制臺窗口中。
E:/test>ildasm SampleClass.exe /text
如果文件 SampleClass.exe 包含嵌入的托管和非托管資源,則下面的命令將產生以下 4 個文件:SampleClass.il、SampleClass.res、Icons.resources 和 Message.resources:
ildasm SampleClass.exe /output:SampleClass.il
下面的命令對 SampleClass.exe 的 SampleClass 類中的 DataClass 方法進行反匯編,并將輸出顯示到控制臺窗口中。
ildasm /item:SampleClass::DataClass SampleClass.exe /text
參數列表:
下列選項可用于 .exe、.dll、.obj 和 .lib 文件。
/output:filename | 創建具有指定 filename 的輸出文件,而不是在對話框中顯示結果。 |
/text | 將結果顯示到控制臺窗口,而不是顯示在對話框中或顯示為輸出文件。 |
/? | 顯示此工具的命令語法和選項。 |
下列附加選項可用于 .exe 和 .dll 文件。
/bytes | 以十六進制格式顯示作為指令注釋的實際字節。 |
/linenum | 包含對原始源行的引用。 |
/nobar | 取消反匯編進度指示器彈出窗口的顯示。 |
/pubonly | 只反匯編公共類型和公共成員。等效于 /visibility:PUB。 |
/quoteallnames | 在單引號中包含所有名稱。 |
/raweh | 以原始格式顯示異常處理子句。 |
/source | 顯示作為注釋的原始源行。 |
/tokens | 顯示類和成員的元數據標記。 |
/visibility:vis [+vis ...] | 只反匯編具有指定可見性的類型或成員。以下是 vis 的有效值。 PUB Public PRI Private FAM Family ASM Assembly FAA Family 和 Assembly FOA Family 或 Assembly PSC Private Scope |
下列選項僅對用于文件或控制臺輸出的 .exe 和 .dll 文件有效。
/all | 指定 /header、/bytes 和 /tokens 選項的組合。 |
/header | 在輸出中包含文件頭信息。 |
/noil | 取消 MSIL 程序集代碼輸出。 |
/unicode | 對輸出使用 Unicode 編碼。 |
/utf8 | 對輸出使用 UTF-8 編碼。默認值是 ANSI。 |
下列選項僅對用于文件或控制臺輸出的 .exe、.dll、.obj 和 .lib 文件有效。
/item:class[::method [(sig)]] | 根據所提供的參數反匯編下列內容:
|
MSIL匯編程序(Ilasm.exe)
下面的命令對 MSIL 文件 SampleClass.il 進行匯編并產生可執行文件 SampleClass.exe。
E:/test>ilasm SampleClass
Microsoft (R) .NET Framework IL Assembler. Version 1.1.4322.2032
Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.
Assembling 'SampleClass.IL' , no listing file, to EXE --> 'SampleClass.EX
Source file is ANSI
Assembled method DataClass::.ctor
Assembled method DataClass::addem
Assembled method SampleClass::.ctor
Assembled method SampleClass::Main
Creating PE file
Emitting members:
Global
Class 1 Fields: 2; Methods: 2;
Class 2 Fields: 2; Methods: 2;
Resolving member refs: 11 -> 11 defs, 0 refs
Writing PE file
Operation completed successfully
下面的命令對 MSIL 文件 SampleClass.il 進行匯編并產生 .dll 文件 SampleClass.dll。
E:/test>ilasm SampleClass /dll
Microsoft (R) .NET Framework IL Assembler. Version 1.1.4322.2032
Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.
Assembling 'SampleClass.IL' , no listing file, to DLL --> 'SampleClass.DLL'
Source file is ANSI
Assembled method DataClass::.ctor
Assembled method DataClass::addem
Assembled method SampleClass::.ctor
Assembled method SampleClass::Main
Creating PE file
Emitting members:
Global
Class 1 Fields: 2; Methods: 2;
Class 2 Fields: 2; Methods: 2;
Resolving member refs: 11 -> 11 defs, 0 refs
Writing PE file
Operation completed successfully
下面的命令對 MSIL 文件 SampleClass.il 進行匯編并產生 .dll 文件 SampleClass.dll。
ilasm SampleClass /dll /output:newSampleClass.dll
參數列表:
/alignment=integer | 將 FileAlignment 設置為由 NT Optional 標題中的 integer 指定的值。如果在文件中指定了 .alignment IL 指令,則此選項將重寫它。 |
/base=integer | 將 ImageBase 設置為由 NT Optional 標題中的 integer 指定的值。如果在文件指定了 .imagebase IL 指令,則此選項將重寫它。 |
/clock | 為指定的 .il 源文件測量并報告下列編譯時間(以毫秒為單位): 總運行時間 執行后面的所有特定操作所花費的總時間。 啟動 加載并打開文件。 分析 發出 MD 發出元數據。 定義引用解析 解析對文件中的定義的引用。修正和鏈接 CEE 文件生成 在內存中生成文件映像。 PE 文件寫入 將映像寫入 PE 文件。 |
/debug | 包括調試信息(局部變量名和參數名以及行號)。 |
/dll | 生成 .dll 文件作為輸出。 |
/exe | 生成可執行文件作為輸出。這是默認值。 |
/flags=integer | 將 ImageFlags 設置為由公共語言運行庫標題中的 integer 指定的值。如果在文件中指定了 .corflags IL 指令,則此選項將重寫它。有關 integer 的有效值的列表,請參見 CorHdr.h 中的 COMIMAGE_FLAGS。 |
/key:keyFile | 使用 keyFile 中包含的私鑰編譯具有強簽名的 filename。 |
/key:@keySource | 使用在 keySource 中生成的私鑰編譯具有強簽名的 filename。 |
/listing | 在標準輸出上生成列表文件。如果省略此選項,則不生成列表文件。 |
/nologo | 取消顯示 Microsoft 啟動標題。 |
/output:file.ext | 指定輸出文件名和擴展名。默認情況下,輸出文件名與第一個源文件名相同。默認擴展名為 .exe。如果指定 /dll 選項,則默認擴展名為 .dll。 注意 指定 /output:myfile.dll 并不會設置 /dll 選項。如果不指定 /dll,則會生成名為 myfile.dll 的可執行文件。 |
/quiet | 指定安靜模式;不報告程序集進度。 |
/resource:file.res | 在生成的 .exe 或 .dll 文件中包括 *.res 格式的指定資源文件。使用 /resource 選項只能指定一個 .res 文件。 |
/subsystem=integer | 將 subsystem 設置為由 NT Optional 標題中的 integer 指定的值。如果在文件中指定了 .subsystem IL 指令,則此命令將重寫它。有關 integer 的有效值的列表,請參見 winnt.h 中的 IMAGE_SUBSYSTEM。 |
/? | 顯示此工具的命令語法和選項。 |
新聞熱點
疑難解答
圖片精選