vnmakarov / mir

A lightweight JIT compiler based on MIR (Medium Internal Representation) and C11 JIT compiler and interpreter based on MIR
MIT License
2.24k stars 145 forks source link

Add support for running MIR binaries, useful for binfmt_misc #389

Closed Wiguwbe closed 5 months ago

Wiguwbe commented 6 months ago

(Tries to solve #378 )

In order to be able to execute .bmir files directly from the command line, a "soft link interface" was added (similar to busybox), where the links c2m-ei, c2m-eg and c2m-el are created, pointing to c2m.

These links, to correspond with the binfmt nomenclature, are referred as interpreters

The c2mir driver source was adapted to check if the argv[0] is any of those links.

Invoking c2m through those links is similar to invoking using

$ c2m /path/to/binary {-ei | -eg | -el} <args>...

To use with binfmt_misc, the driver expects the P flag on the binfmt format, that is, the first argument should be the full path to the executable binary.

./a.bmir <args>...
c2m-ei /path/to/a.mir ./a.mir <args...>

or if the binary is in PATH

a.bmir
c2m-ei /usr/local/bin/a.mir a.mir

The binfmt line is (echo it to /proc/sys/fs/binfmt_misc/register as root, or add it to /etc/binfmt.d/)

:mir:M::MIR:/usr/local/bin/c2m-el:P

Do adapt the path where the c2m-* are and possibly change -el to your favorite interface (interpeted, generated or lazy)

Wiguwbe commented 5 months ago

A new "interface" was added, mir-run on latest commit:

As a follow up on binfmt_misc, a new mir-run tool is created to handle the running of bmir binaries.

The MIR execution type is defined by the MIR_TYPE environment variable, defaulting to interp (other options are jit for generation and lazy for lazy code generation).

This tool also accepts extra libraries to be loaded using the MIR_LIBS environment variable and extra library paths to search through environment variables LD_LIBRARY_PATH and MIR_LIB_DIRS (besides the default ones).

Wiguwbe commented 5 months ago

Note: library loading is still a work in progress...

Wiguwbe commented 5 months ago

Just a minor fix needed after all

vnmakarov commented 5 months ago

Thank you for the pull request implementing an interesting feature. I'll review it on this week. If it is ok for me, I'll merge it.

I'll probably do some additional changes in doc (and may be in makefile) after that as this feature is not portable. Still it is pretty interesting. Thank you for the PR again.

vnmakarov commented 5 months ago

Sorry for the delay. I was and still am very busy.

Thank you for the PR. I've merged it.

edubart commented 5 months ago

Would be nice to also have this available in bbv branch.