Linux – How does Linux determine what facilities to use to run a (non-text) binary

executablelinux

I understand that Linux uses shebang line to determine what interpreter to use for scripting languages, but how does it work for binaries?

I mean I can run Linux binaries, and having installed both wine and mono, Windows native and .NET binaries. And for all of them it's just ./binary-name (if not in PATH) to run it.

How does Linux determine that a given binary must be run as a Linux native binary, as a Windows native binary (using wine facilities) or as a Windows .NET binary (using mono facilities)?

Best Answer

In a word: binfmt_misc. It's a Linux-specific, non-portable, facility.

There are a couple of formats that are recognized by the kernel with built-in logic. Namely, these are the ELF format (for normal binaries) and the shebang convention (for scripts). (thanks to zwol for the following part of the answer). In addition, Linux recognizes a couple of esoteric or obsolete or compatibility builtin formats. You probably won't encounter them. They are a.out, "em86", "flat", and "elf_fdpic".

Everything else must be registered through the binfmt_misc system. This system allows you to register with the kernel a simple pattern check based on a magic number, and the corresponding interpreter.

Related Question