The .exe file in the above listing contains the managed
code in CIL format, while the hello.exe.so is an ELF file that
contains the pre-compiled code.

Today, the .so file only contains the pre-compiled
code but does not contain any of the additional metadata that
is present on the .exe file. To run the application it is
necessary to have the original assembly (.exe or .dll) around
as it contains information that is not replicated on the
executable.

To execute the code, you must still invoke Mono with the
original parameters, for instance:

$ mono hello.exe

The runtime will take care of probing whether there is a
native pre-compiled image, and if the image is valid, it will
use the pre-compiled code from the shared object instead of
JITing the code as it goes.

As I mentioned before, there were an array of motivations
for implementing AOT in Mono. Initially we hoped to reduced
application startup time as the JIT had less work to do. In
practice, the Mono JIT was fast enough that the JIT time was
never much of an issue, even with the last batch of
optimizations that were turned on in Mono 1.1.16, the startup
speed is not even noticeable. In addition, there is a slight
memory gain by not executing code in the JIT in the first
place, so there was a small memory improvement as well.

Heavier optimizations that exist today, and heavier
optimizations that we are developing will make the use of AOT
more important, as those code generation optimizations are
slower to have enabled by default on the Just-in-Time
compilation stage.

This is what the original design of the AOT file format was
designed to cope with: startup time.

In the last few months Zoltan has been working on adapting
AOT for another task: overall memory reduction.

As developers start to run a handful of Mono applications
on a desktop, we want to minimize the duplicated code in
memory. To do this, we had to address a number of problems
with the original AOT design.

The original AOT design had poor multi-process page sharing
capabilities, as the JIT had to fix-up the mapped AOT code in
memory. The native code was shared across multiple processes
by using mmap() to map the native code into each
process, but as the fix-ups were applied to the mapped code,
the numbers of pages that could be shared across processes
decreased.

The new design by Zoltan for the AOT generated code is very
similar to the ELF shared libraries file format. It uses a
couple of tables for data and code (Global Offset Table a
slightly modified Program Linkage Table tuned for Mono
compilation) which are the only pieces that are modified.

By reducing the pieces that have to be patched to a small
set of pages, the rest of the code can be shared across
multiple Mono processes.

Now, with the same set of optimization flags, AOT code is
slightly slower than JITed code because it has to be position
independent and has to go through a few extra indirections
that JIT code does not have to do (to maximize the sharing
across applications).

The big news is that the new file format design has finally
reached the point where using AOT code on day to day
operations is faster than using JIT code. Building our core
class libraries with our C# compiler with AOT code now takes
less time than using the JIT-tuned version.

Today AOT is only available on the x86 and x86-64 ports of
Mono, this seems to be good enough and where most of the
desktop deployments will be.

Now, one common question we get is: if we already generate
native code, how come you still need Mono to run your
applications; or the variant of the question: can I produce
statically generated executables that only contain native
code?

It is currently not possible to generate merely a native
code file because Mono still needs to extract the metadata
from the original assembly file (the .dll or the .exe file),
information that we do not encode in the AOT file format. In
addition the generated image still requires the Mono runtime
(for GC, Just-in-Time compilation services, reflection and a
handful of extra features).

Mono does support a special mode that turns an assembly
into a static binary, but it does not use native code, it
still uses the JIT engine. This is done using
the mkbundle
command.

It is still possible to turn off the use of AOT files in a
per-run basis, this can be done by passing the -O=-aot
command line option to the runtime. In the upcoming version
of Mono, all of our wrapper scripts will also support the
MONO_OPTIONS variable to control this.

We are hoping to ship a script that will AOT all the
libraries and executables that ship as part of Mono, or to
disable all the AOT executables on demand in a future release
of Mono.

Don asked me at the Lang.NET Symposium how we handled
generics, and I have no idea.