Search code examples
c#monoaot

Mono AOT Segmentation Fault - Strange Problem


I wanted to test Mono AOT, so I wrote a simple console application with MonoDevelop:

using System;

namespace abc
{
    public class Program
    {
        public static void Main()
        {
            Console.WriteLine("Hello World!");
        }
    }
}

It created a file named "abc.exe" in bin/debug. When I try to do:

mono abc.exe

it worked. So, I tried to AOT it. I did:

mono --aot abc.exe

it created me a file named abc.exe.so. When I try to start it, it throws an "Segmentation fault (core dumped)" error. Heres the output of mono --aot:

Code: 51 Info: 5 Ex Info: 8 Unwind Info: 31 Class Info: 30 PLT: 2 GOT Info: 11 GOT Info Offsets: 16 GOT: 20 Offsets: 48
Compiled 2 out of 3 methods (66%)
1 methods have other problems (33%)
Methods without GOT slots: 1 (33%)
Direct calls: 0 (100%)
JIT time: 1 ms, Generation time: 0 ms, Assembly+Link time: 0 ms.

How can I fix it?

Thanks.


Solution

  • What skolima said was right, it is quite odd that he got down-voted.

    You are not supposed to use directly the output generated by Mono's AOT compiler.

    Mono's AOT compilation is a process that caches the output of JIT compilation and leaves it in a shared library that is loaded by the Mono runtime on demand. The .so file that is generated contains the pre-compiled code and the metadata necessary to be consumed by the runtime.

    To use the AOT file, you just invoke your program like this:

    mono program.exe

    Mono will load the metadata from program.exe and probe for the existence of a program.exe.so to avoid doing just-in-time compilation at runtime, and instead use the code from the .so file. The same process is applied to any libraries referenced.

    Even Mono's full static compilation mode produces code that requires the original .exe file to be present.

    If what you wanted to achieve was a native executable, you could try the mkbundle command which packages the Mono runtime and your program into a single executable.