Mono Mkbundle produces its own code

The normal exe generated by mono is in IL.I want to generate Native Executable using mkbundle to produce native Exe or use Mono AOT.

+6
source share
2 answers

Yes, mkbundle creates its own executables. For example, on Linux, here is my .NET assembly:

file Agent.exe Agent.exe: PE32 executable (console) Intel 80386, Mono/.Net assembly, for MS Windows 

I mkbundle compile it for my own executable file (here I need to add Common.dll, which is a dependency of my Agent.exe assembly):

  mkbundle --deps -o Agent Agent.exe Common.dll
    OS is: Linux
    Sources: 3 Auto-dependencies: True
    embedding: Agent / bin / Debug / Agent.exe
    embedding: Agent / bin / Debug / Common.dll
    embedding: /usr/lib/mono/4.5/mscorlib.dll
    embedding: /usr/lib/mono/gac/System.Runtime.Serialization/4.0.0.0__b77a5c561934e089/System.Runtime.Serialization.dll
    embedding: /usr/lib/mono/gac/System.Xml/4.0.0.0__b77a5c561934e089/System.Xml.dll
    embedding: /usr/lib/mono/gac/System/4.0.0.0__b77a5c561934e089/System.dll
    embedding: /usr/lib/mono/gac/Mono.Security/4.0.0.0__0738eb9f132ed756/Mono.Security.dll
    embedding: /usr/lib/mono/gac/System.Configuration/4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
    embedding: /usr/lib/mono/gac/System.Security/4.0.0.0__b03f5f7f11d50a3a/System.Security.dll
    embedding: /usr/lib/mono/gac/System.Core/4.0.0.0__b77a5c561934e089/System.Core.dll
    embedding: /usr/lib/mono/gac/Mono.Posix/4.0.0.0__0738eb9f132ed756/Mono.Posix.dll
    embedding: /usr/lib/mono/gac/System.Data.Linq/4.0.0.0__b77a5c561934e089/System.Data.Linq.dll
    embedding: /usr/lib/mono/gac/System.Data/4.0.0.0__b77a5c561934e089/System.Data.dll
    embedding: /usr/lib/mono/gac/Mono.Data.Tds/4.0.0.0__0738eb9f132ed756/Mono.Data.Tds.dll
    embedding: /usr/lib/mono/gac/System.Transactions/4.0.0.0__b77a5c561934e089/System.Transactions.dll
    embedding: /usr/lib/mono/gac/System.EnterpriseServices/4.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll
 Compiling:
 as -o temp.o temp.s 
 cc -ggdb -o Agent -Wall temp.c `pkg-config --cflags --libs mono-2` temp.o
 Done

Now, run the file command again, this time against the generated binary agent:

 file Agent Agent: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=698384c13208eccc609e5a573deeb09ed3420a29, not stripped 

Note: this native binary is still dependent on libmono (the Mono runtime library), so it will not work on a machine without a working Mono installation.

However, you can embed libmono in your binary and get an independent standalone executable using the mkbundle --static .

+14
source

mkbundle creates an executable program that will contain static copies of the assemblies listed on the command line.

I doubt that this is what you expected from the word "native exe". Here's a little experiment:

Test01.cs

 using System; public class Test01 { public static void Main() { Console.WriteLine("Hello, world!"); Console.WriteLine("Hello, world!"); Console.WriteLine("Hello, world!"); } } 

The output MSIL should contain three pairs of ldstr and call instructions and a final ret statement. Their operation codes are 0x72, 0x28 and 0x2a respectively:

Let the hex dump check be done ( grep -P allows you to use an undesired *? match)

 $ od -t x1 -w9999999 Test01.exe | grep -o -P 72.*?28.*?2a ... 72 01 00 00 70 28 01 00 00 0a 72 01 00 00 70 28 01 00 00 0a 72 01 00 00 70 28 01 00 00 0a 2a 

Now we know the MSIL byte sequence corresponding to Test01.cs above. And the output from mkbundle contains exactly the same sequence of bytes:

 $ mkbundle -o Test01 Test01.exe --deps ... $ od -t x1 -w9999999 Test01 | fgrep -o '72 01 00 00 70 28 01 00 00 0a 72 01 00 00 70 28 01 00 00 0a 72 01 00 00 70 28 01 00 00 0a 2a' 72 01 00 00 70 28 01 00 00 0a 72 01 00 00 70 28 01 00 00 0a 72 01 00 00 70 28 01 00 00 0a 2a 

Unfortunately, I don't think mkbundle(1) has any options like --full-aot . Thus, AOT and the creation of a (static) exe package are mutually exclusive at the moment (Mono 4.2.2.30)

+3
source

All Articles