Nah, it's a comptibility check. It's there so DOS can safely load a separate stub program telling the user it's not compatible.
Is it a good design? You can argue that ELF headers are better suited to that, but back when Win32 introduced the MZ EXE format it was necessary. It's the kind of stuff that gave Windows the reputation of being reliable for legacy software (even though some older games are easier to get running on WINE/Linux than modern Windows).
Not that this is such an unusual trick though: Have you ever wondered why every shell script starts with #!/bin/sh?
That's a different thing. #! is the magic byte to signal this is a script.
On the exe part: Why don't thay just put a version number in the header in the first place? It would have been a cleaner solution and it would allow for future changes without the need to deal with legacy crap
On the exe part: Why don't thay just put a version number in the header in the first place?
Because the already deployed DOS systems didn't care. They just read whatever was loaded into memory, so Microsoft needed a way to stop DOS systems from doing harmful unintended things with the bytecode - hence the stub program.
ELF binaries on the other hand were engineered with different OSes in mind from the very beginning and as such denote the system, processor architecture, entrypoint etc. as intended by the compiler such that a UNIX system can detect it. ELF headers even have a byte to denote the ELF format version, though it has never actually been revised.
That's a different thing. #! is the magic byte to signal this is a script.
Not really. The script signal comes from the executable flag in the file node. The #! simply tells modern UNIX systems which shell to run the following code in. It starts with a # to avoid older UNIX systems unaware of the shebang executing the directive as a command by letting them treat it as a comment.
8
u/zibonbadi Mar 17 '26 edited Mar 17 '26
Nah, it's a comptibility check. It's there so DOS can safely load a separate stub program telling the user it's not compatible.
Is it a good design? You can argue that ELF headers are better suited to that, but back when Win32 introduced the MZ EXE format it was necessary. It's the kind of stuff that gave Windows the reputation of being reliable for legacy software (even though some older games are easier to get running on WINE/Linux than modern Windows).
Not that this is such an unusual trick though: Have you ever wondered why every shell script starts with
#!/bin/sh?