.Net-Core: Equivalent of Ildasm/Ilasm

.net-core: Equivalent of ILDASM / ILASM

Both the ildasm and ilasm tools are built with CoreCLR from this repo: https://github.com/dotnet/coreclr. They include similar functionality as the versions shipped with Windows (sans GUI, etc.).

There are nuget packages shipped that include them as well (https://www.nuget.org/packages?q=ildasm), but they are platform-specific and also require a matching version of CoreCLR to use, so they are not straightforward to consume via nuget. The easiest way to run these on your platform is to just build them from source from the coreclr repo.

Is there an ILDASM tool for dotnet 5 or 6?

ilasm and ildasm are available as nuget packages on nuget.org. Look for runtime.{RUNTIME_ID}.Microsoft.NETCore.ILAsm and runtime.{RUNTIME_ID}.Microsoft.NETCore.ILDAsm packages.

For example:

  • Linux x64: https://www.nuget.org/packages/runtime.linux-x64.Microsoft.NETCore.ILDAsm/
  • macOS arm64: https://www.nuget.org/packages/runtime.osx-arm64.Microsoft.NETCore.ILDAsm/
  • Windows x64: https://www.nuget.org/packages/runtime.win-x64.Microsoft.NETCore.ILDAsm/

These contain executibles built for each specific .NET (and .NET Core) version for each platform.

How to use .NET Core 2.0 ilasm.exe?

The .NET Core variants of ILAsm and ILDasm depend on core assets of the build of the same CoreCLR version they are built for. If they are restored via a dependency graph, a self-contained .net core deployment depending on Microsoft.NETCore.ILAsm should contain all assets for the target runtime.

To get the extracted version of ilasm.exe / ildasm.exe to work without doing that, you need to copy the coreclr.dll from the matching .net core version into the same directory. This file can be found in C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.0.0 if a .NET Core 2.0.0 runtime is installed in its default location.

Where is ilasm.exe in Microsoft.NETCore.ILAsm?

This package makes use of a new feature in NuGet to split packages per runtime. At the root of the Microsoft.NETCore.ILAsm package, you'll find a runtime.json file that references other nuget packages per platform. This means that when this package is used for runtime-specific actions, another referenced one is used.

For 64 bit windows, this json file contains:

"win-x64": {
"Microsoft.NETCore.ILAsm": {
"runtime.win-x64.Microsoft.NETCore.ILAsm": "2.0.0"
}
},

So you have to download the runtime.win-x64.Microsoft.NETCore.ILAsm nugget package instead which then contains a runtimes/win-x64/native/ilasm.exe file to use.

ildasm on Linux via nuget installation: ildasm executable not found

Let's consider two ways to install ildasm.

Using nuget-package

  • define RID (Runtime Identifier)
dotnet --info

# execution result
..
Runtime Environment:
OS Name: ubuntu
OS Version: 18.04
OS Platform: Linux
RID: ubuntu.18.04-x64 # <----
..
  • download the package runtime.{RID}.Microsoft.NETCore.ILDAsm. For my case it is: runtime.ubuntu.18.04-x64.Microsoft.NETCore.ILDAsm
  • unarchive it and extract executable file '/runtimes/{RID}/native/ildasm'
  • grant it execution permission and copy to .NET runtime folder (call dotnet --list-runtimes to list runtimes)
chmod +x ildasm
sudo mv ildasm /usr/share/dotnet/shared/Microsoft.NETCore.App/{version}/
  • create symlink
ln -s /usr/share/dotnet/shared/Microsoft.NETCore.App/{version}/ildasm ildasm
  • run ildasm
./ildasm {path}/project.dll >> {path}/project.il

The same steps are applicable for ilasm.

Using dotnet-ildasm tool

# install .net core runtime if required
# sudo apt-get update; \
# sudo apt-get install -y apt-transport-https && \
# sudo apt-get update && \
# sudo apt-get install -y dotnet-runtime-3.0

# find required tool
dotnet tool search ildasm
# output:
# Package ID Latest Version Authors Downloads Verified
# ---------------------------------------------------------------------------
# dotnet-ildasm 0.12.2 pjbgf 100154
# dotasm 1.0.1 DotAsm 434

# install tool
dotnet tool install -g dotnet-ildasm

Output IL to file:

# go to project folder
cd ../project/bin/Debug/netx.x

dotnet ildasm program.dll -o program.il

Does Mono have the equivalent of ILDASM?

Yes, monodis is Mono's equivalent for ildasm.


$ cat a.cs
public class Foo
{
public static void Main()
{
System.Console.WriteLine("Hello world");
}
}

$ monodis a.exe
.assembly extern mscorlib
{
.ver 1:0:5000:0
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
}
.assembly 'a'
{
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module a.exe // GUID = {034ADE1A-22D2-4B2B-960B-AE9DBFB2FCE7}

.class public auto ansi beforefieldinit Foo
extends [mscorlib]System.Object
{

// method line 1
.method public hidebysig specialname rtspecialname
instance default void '.ctor' () cil managed
{
// Method begins at RVA 0x20ec
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void object::'.ctor'()
IL_0006: ret
} // end of method Foo::.ctor

// method line 2
.method public static hidebysig
default void Main () cil managed
{
// Method begins at RVA 0x20f4
.entrypoint
// Code size 11 (0xb)
.maxstack 8
IL_0000: ldstr "Hello world"
IL_0005: call void class [mscorlib]System.Console::WriteLine(string)
IL_000a: ret
} // end of method Foo::Main

} // end of class Foo

ILDASM and ILASM, how use them?

simple, let's take this line of code:

using System;      
public class sample
{
static void Main(string[] args)
{
Console.WriteLine("Inside main ...");
}
}

Let us compile App.cs to App.exe. This executable now prints Inside main … when we run it.

Now let us attempt to change the output of this executable without touching its source files.

You run ildasm App.exe /out:App.il to generate the IL code corresponding to the executable. You can now edit this il code in notepad and change the ldstr from “Inside main …” to “Inside main changed …” as shown below:

IL_0000:  nop
IL_0001: ldstr "Inside main ..."
IL_0006: call void [mscorlib]System.Console::WriteLine(string)
IL_000b: nop
IL_000c: ret

To

IL_0000:  nop
IL_0001: ldstr "Inside main changed ..."
IL_0006: call void [mscorlib]System.Console::WriteLine(string)
IL_000b: nop
IL_000c: ret

Now let us rebuild the executable from this il file by running ilasm App.il. This generates a new App.exe. If you run App.exe you will get the following output “Inside main changed …”

There are useful posts in social.msdn and in c-sharpcorner as well guiding through the use of ILDASM and ILASM.



Related Topics



Leave a reply



Submit