I have read that a binary is the same for Windows and Linux (not sure if it has a different format)
My question is, what are the differences between binaries for Linux and binaries for Windows (when speaking about format),
And if there are none, what stops us from making a single binary for both operating systems (make sure that I mean the last file where you can run and not the source code)
Since there are differences in the binary format, how are the operating systems themselves compiled?
If I'm not mistaken Microsoft uses windows to compile the next windows version/update
How are these binaries executable by the machine (even the kernel is one of those)
Aren't they in the same format? (For such a low level program)
CodePudding user response:
As mentioned in the comments by @AlanBirtles, this is in fact possible. See Actually Portable Executable, Redbean, and the article about the two.
Even though the binary formats are different, it's possible to come up with a file that's valid in several different formats.
But, this being an obscure hack, I would stay away from it in production (or at all).
CodePudding user response:
I have read that a binary is the same for Windows and Linux (not sure if it has a different format)
Then you have read wrong. They are completely different formats.
My question is, what are the differences between binaries for Linux and binaries for Windows (when speaking about format),
Windows: https://en.wikipedia.org/wiki/Portable_Executable
Linux: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
And if there are none
There are many differences.
what stops us from making a single binary for both operating systems
The fact that different OSes require different binary formats for their respective executable files.
Since there are differences in the binary format, how are the operating systems themselves compiled?
Ask the OS manufacturers for details. But basically, they are compiled like any other program (just very complex programs). Like any other program, their source code is compiled into appropriately-formatted executable files for each platform they are targeting, and even for each target CPU. For instance, Windows uses the PE format for all of its executables, but the underlying machine code inside each executable is different whether the executable is running on an x86 CPU vs an x64 CPU vs an ARM CPU.
If I'm not mistaken Microsoft uses windows to compile the next windows version/update
Yes. That is commonly known as "dog-fooding" or "bootstrapping". For instance, VC running on Windows is used to develop new versions of VC , Windows, etc.
How are these binaries executable by the machine (even the kernel is one of those)
When an executable file is run (by the user, by a program, etc), a request goes to the OS, and the OS's executable loader then parses the file's format as needed to locate the file's machine code, and then runs that code on the target CPU(s) as needed.
As for running the user's OS itself, there is an even more fundamental OS running on the machine (ie, the BIOS) which (amongst other things) loads the user's OS at machine startup and starts it running on the available CPU(s). See Booting an Operating System for more details about that.
Aren't they in the same format? (For such a low level program)
No.
CodePudding user response:
An executable binary compiled for Linux x86-64 (see elf(5)...) won't run on the same computer with Windows (unless you use some emulator which luckily works for your binary, like Wine or QEMU).
Using emulators will slow down the execution. Some emulators don't emulate every aspect of a computer in 2022.
This is one of the reasons I prefer open source application software (e.g. RefPerSys or GCC) ..... With efforts and good design you can (in principle) port them to another platform.
Some corporations designed some kind of fat binary formats. They did not succeed a lot.
Levine's book about Linkers and loaders explain several different binary formats.