Most people meet IL assembly as a listing in a decompiler or the output of a disassembler; a few write it by hand for tests, for code that C# cannot express, or to patch an assembly. The two tools that define the format are ildasm (assembly to text) and ilasm (text to assembly). Both are built from the open dotnet/runtime repository (src/coreclr/ildasm and src/coreclr/ilasm), and the NuGet packages Microsoft.NETCore.ILAsm and Microsoft.NETCore.ILDAsm (and the runtime.rid.Microsoft.NETCore.ILAsm packages for each platform, for example runtime.osx-arm64) carry them; their version lists on nuget.org run up to 11.0.0.
// a round trip with the usage text of the two tools
// ildasm /OUT=Hello.il Hello.dll
// (edit Hello.il)
// ilasm Hello.il /DLL /OUTPUT=Hello2.dll
.assembly extern mscorlib {}
.assembly Hello {}
.module Hello.dll
/OUT= of ildasm, /DLL and /OUTPUT= of ilasm; with a dash on macOS and Linux); the three lines of IL are the start of a listing and assembled with ilasm 10.0.15.ilasm
ilasm [options] source file [options]. Options start with - or / and are recognised by their first three letters (except ARM and ARM64); the default source extension is .il. On macOS and Linux write the dash: a leading / is taken as the start of a file name, so ilasm /NOLOGO /DLL x.il answers “Could not open /NOLOGO.il” and so on for each option (seen with ilasm 10.0.15 on macOS; ildasm 10.0.15 given /UTF8 /OUT=x.il printed only its banner and wrote no file). From the usage text of the program (the same text in 6.0.0 to 11.0.0, except the three differences listed after the table):
| Option | Meaning |
|---|---|
/NOLOGO, /QUIET | no logo; no progress report |
/NOAUTOINHERIT | do not make System.Object the default base type |
/DLL, /EXE | write a library, or an executable (the default) |
/OUTPUT=file | the name of the output file (with its extension) |
/PDB, /DEBUG, /DEBUG=IMPL, /DEBUG=OPT | write a PDB file; with /DEBUG the JIT optimisation is off and the sequence points come from the PDB (IMPL: implicit sequence points; OPT: optimisation stays on) |
/DET | deterministic output: with it the MVID is the same in every run (the same GUID in two runs of ilasm 10.0.15), but two output files still differed in one byte in 10.0.15 and in 11.0.0 on macOS arm64, so the files were not identical |
/OPTIMIZE, /FOLD | turn long instructions into short ones; merge identical method bodies |
/KEY=keyfile, /KEY=@source | sign the assembly with a strong name from a key file or key source (a missing file gives “Failed to open key file”; the helper that built the signed fixture of this site records in dotnet/SOURCES.md that ilasm on CoreCLR cannot do the signing, and made that assembly with Roslyn) |
/ANAME=name | override the name of the assembly |
/INCLUDE=path | folders to search for #include files |
/SUBSYSTEM=n, /SSVER=a.b, /FLAGS=n, /ALIGNMENT=n, /BASE=n, /STACK=n | the PE and CLR header fields that .subsystem, .corflags, .file alignment, .imagebase and .stackreserve set, and the subsystem version |
/MDV=version, /MSV=a.b | the metadata version string; the metadata stream version |
/PE64, /X64, /ARM, /ARM64, /32BITPREFERRED | the kind of image: PE32+; 64-bit AMD; ARM; ARM64; a 32-bit-preferred PE32 (/PE64 implies /X64 and /ARM64 implies /PE64; with /PE64 alone ilasm 10.0.15 says “Machine type /ARM64 or /X64 must be specified for 64 bit targets. Type set to X64.” and the file has Machine 0x8664 and the PE32+ magic 0x020b, as ildasm’s /HEADERS shows) |
/APPCONTAINER, /HIGHENTROPYVA, /NOCORSTUB, /STRIPRELOC, /CLOCK | an AppContainer image; high-entropy address space; no CORExeMain stub; no base relocations needed; report the time of each stage |
Changes in the usage text between releases (each release’s ilasm -? and ildasm -? were compared, 6.0.0 to 11.0.0): the banner “Microsoft (R) .NET IL Assembler” became “.NET IL Assembler” in 7.0.0, and the copyright line changed in 8.0.0; /DET and /ANAME are in every 10.0.0 preview from the first (10.0.0-preview.1) but not in 9.0.0; nothing else in the options of ilasm changed from 6.0.0 to 11.0.0. For ildasm only /R2RNATIVEMETADATA was added, in 7.0.0 (6.0.0 has an unfinished line “[Undefined resource string ID:0xDB]” in its place, and every release prints “Undefined resource string ID:0xCC” before /HTML). Tested with ilasm 10.0.15: -ANAME=Renamed renamed the assembly, -PDB and -DEBUG each wrote a .pdb file next to the output, -ARM64 and -X64 assembled, and -NOAUTOINHERIT, -OPTIMIZE and -FOLD were accepted.
ildasm
ildasm [options] file [options]; the options also begin with - or / and are recognised by their first three letters.
| Option | Meaning |
|---|---|
/OUT=file, /HTML, /RTF | write to a file instead of the console; HTML and rich text are valid with /OUT only |
/BYTES, /TOKENS, /SOURCE, /LINENUM | show the bytes of each instruction as comments; the metadata tokens; the source lines as comments; references to the source lines |
/RAWEH | show exception clauses in raw form |
/VISIBILITY=vis[+vis…], /PUBONLY | only items with the visibility PUB, PRI, FAM, ASM, FAA, FOA or PSC; only public items |
/QUOTEALLNAMES | put every name in single quotes |
/NOCA, /CAVERBAL | no custom attributes; custom attribute blobs in the verbal form instead of bytes |
/UTF8, /UNICODE | the encoding of the output (the default is ANSI) |
/NOIL, /FORWARD, /TYPELIST, /PROJECT | no IL code; forward class declarations; the full list of types to keep their order in a round trip; the projected view of a .winmd file |
/HEADERS, /STATS, /CLASSLIST, /ALL | file header information; statistics; the list of classes; all of /HEADER, /BYTES, /STATS, /CLASSLIST and /TOKENS |
/ITEM=class[::method[(sig)]] | disassemble one class or method |
/METADATA[=specifier], /R2RNATIVEMETADATA | show the metadata (MDHEADER, HEX, CSV, UNREX, SCHEMA, RAW, HEAPS, VALIDATE); the metadata of the ReadyToRun native manifest |
Mono and other tools
| Tool | What it does with IL |
|---|---|
Mono ilasm and monodis | Mono’s assembler and disassembler (the dialects) |
| ILSpy, dnSpy, SharpLab and similar decompilers | show an assembly as C#, Visual Basic or F# and also as IL; their IL is not always ilasm-ready text |
Mono.Cecil, System.Reflection.Emit, System.Reflection.Metadata | libraries that read or write assemblies and IL instructions without text |
| this site’s .NET assembly viewer | lists method bodies in the style of ildasm (explorer/formats/dotnet/listing.js: the .method header, // Code size, .maxstack, .locals, one instruction a line, try and handler blocks in braces); its opcode reference shares the instruction table with the .il viewer |
Not described
Known, not yet covered
- Reference: the command-line options of Mono’s
ilasmandmonodiswere not read (only their lexer tables and the listing code). The IL views of ILSpy, dnSpy and SharpLab were not compared with ilasm’s syntax.
Unknown
- Reference: only ilasm and ildasm of .NET 6.0.0 to 11.0.0 for macOS arm64 were run; the Windows and Linux builds, the .NET Framework tools, Mono and the decompilers were not. The effect of most ildasm options (
/VISIBILITY,/SOURCE,/HTMLand/RTFand others) on the text was not examined one by one; those that change the text were run to build the test corpus (/BYTES /TOKENS /CAVERBAL /QUOTEALLNAMES /TYPELIST /FORWARD /RAWEH /NOCA /LINENUM /PUBONLY /ALL).