{{Short description|Binary file format}} {{DISPLAYTITLE:binfmt_misc}} '''binfmt_misc''' (''Miscellaneous Binary Format'') is a capability of the Linux kernel which allows arbitrary executable file formats to be recognized and passed to certain user space applications, such as emulators and virtual machines.<ref>[https://www.kernel.org/doc/html/latest/admin-guide/binfmt-misc.html Kernel Support for miscellaneous Binary Formats (binfmt_misc)] The Linux Kernel documentation</ref> It is one of a number of binary format handlers in the kernel that are involved in preparing a user-space program to run.<ref>[https://lwn.net/Articles/630727/ How programs get run], January 28, 2015, by David Drysdale, LWN.net</ref>
The executable formats are registered through the special purpose file system <code>binfmt_misc</code> file-system interface (usually mounted under part of {{nowrap|/proc}}). This is either done directly by sending special sequences to the <code>register</code> procfs file or using a wrapper like Debian-based distributions ''binfmt-support'' package<ref>[http://packages.debian.org/sid/binfmt-support Details of package binfmt-support in sid], Debian</ref> or systemd's <code>systemd-binfmt.service</code>.<ref>[https://www.freedesktop.org/software/systemd/man/systemd-binfmt.service.html# systemd-binfmt.service], freedesktop.org</ref><ref>[https://www.freedesktop.org/software/systemd/man/binfmt.d.html binfmt.d], freedesktop.org</ref>
== Registration == According to the [https://docs.kernel.org/admin-guide/binfmt-misc.html kernel docs] an executable file type can be added by writing a line to the <code>register</code> file, of the form:
<code>:name:type:offset:magic:mask:interpreter:flags</code>
* <code>name</code> is the name of the binary format. * <code>type</code> is either <code>E</code> or <code>M</code> ** If it is <code>E</code>, the executable file format is identified by its filename extension: <code>magic</code> is the file extension to be associated with the binary format; <code>offset</code> and <code>mask</code> are ignored. ** If it is <code>M</code>, the format is identified by <code>magic</code> number at an absolute <code>offset</code> (defaults to <code>0</code>) in the file and <code>mask</code> is a bitmask (defaults to all <code>0xFF</code>) indicating which bits in the number are significant. * <code>interpreter</code> is a program that is to be run with the matching file as an argument. * <code>flags</code> (optional) is a string of letters, each controlling a certain aspect of <code>interpreter</code> invocation: ** <code>P</code> to preserve the original program name typed by user in command line — by adding that name to <code>argv</code>; the <code>interpreter</code> has to be aware of this so it can correctly pass that additional parameter to the interpreted program as its <code>argv[0]</code>. ** <code>O</code> to open the program file and pass its file descriptor to the <code>interpreter</code> so it could read an otherwise unreadable file (for which the user does not have the "Read" permission). ** <code>C</code> to determine new process credentials based on program file rather than <code>interpreter</code> file (see setuid); implies <code>O</code> flag. ** <code>F</code> to make the kernel open the binary at configuration time instead of lazily at startup time, so that it is available inside other mount namespaces and chroots as well.
Each format has a corresponding file entry in the <code>/proc/sys/fs/binfmt_misc</code> directory which can be read to get information about a given file format, or written to disable, enable or deregister it.
== Deregistration == {{sxhl|2=console| $ echo -1 >/proc/sys/fs/binfmt_misc/status # all entries $ echo -1 >/proc/sys/fs/binfmt_misc/qemu-xtensa # single entry }}
== Common usage == * binfmt_misc allows Java programs to be passed directly to the Java virtual machine.<ref>[https://www.kernel.org/doc/html/latest/admin-guide/java.html Java Binary Kernel Support for Linux v1.03] The Linux Kernel documentation</ref> * binfmt_misc allows identification of PE executables using the magic number "MZ". Examples: ** <code>:DOSWin:M::MZ::/usr/bin/wine:</code> will hand all such files to Wine (thus assuming they are ordinary MS-DOS or Microsoft Windows executable) ** <code>:CLR:M::MZ::/usr/bin/mono:</code> will hand such files to Mono (thus assuming they are .NET executables)<ref>[https://www.kernel.org/doc/html/latest/admin-guide/mono.html Mono Binary Kernel Support for Linux] The Linux Kernel documentation</ref><ref>[http://www.mono-project.com/Guide:Running_Mono_Applications Guide:Running Mono Applications]</ref> ** Microsoft's WSL1 uses binfmt to allow for calling Windows programs from Linux. The line is equivalent to <code>:WSLInterOP:M::MZ::/init:P</code>. * binfmt_misc can also be combined with QEMU's user-mode emulation or Box86 to execute programs for other processor architectures as if they were native binaries.<ref>{{Citation|title=Official QEMU mirror|date=23 November 2022 |url=https://github.com/qemu/qemu/blob/f31160c7d1b89cfb4dd4001a23575b42141cb0ec/qemu-doc.texi#L2590|publisher=QEMU}}</ref> * binfmt can be used to turn some compiled languages such as Go into scripting languages, acting as a substitute for the shebang line.<ref>{{Citation|title=Using Go as a scripting language in Linux|date=2018-02-20|url=https://blog.cloudflare.com/using-go-as-a-scripting-language-in-linux/|publisher=Cloudflare|access-date=2019-05-05}}</ref> * binfmt_misc can be used on NixOS to run AppImage files with an interpreter.<ref>{{Cite web|title=Appimage - NixOS Wiki|url=https://nixos.wiki/wiki/Appimage|website=nixos.wiki|access-date=2025-11-27}}</ref>
==References== <references />
==External links== {{Portal|Free and open-source software}} * [https://web.archive.org/web/20100118183358/http://www.tat.physik.uni-tuebingen.de/~rguenth/linux/binfmt_misc.html Project home page] Archived on archive.org
{{Linux kernel}}
{{DEFAULTSORT:Binfmt Misc}} Category:Linux kernel features Category:Free special-purpose file systems Category:Articles with underscores in the title