3 ms·
I may have misunderstood, but I'm pretty sure APE is not a "binary format" per se. It is a script that can be executed on any system. That script can then LOAD
by vitiral 3y ago
I may have misunderstood, but I'm pretty sure APE is not a "binary format" per se.
It is a script that can be executed on any system. That script can then LOAD a binary. IIRC the original needed to decode it from base64 before it could be loaded.
So... it's an executable binary loader
- rep_lodsb 3y agoIt's a script that starts with an EXE header ("MZ"). Having both EXE and ELF headers at the same location is obviously impossible, since they start with different bytes. A possible, although very limited way to have an actual binary program execute on different platforms would be to create a DOS .COM file (which has no header, just the raw machine code) with a valid ELF header. It would then also work on 32-bit Windows via its built-in DOS emulator, and presumably on 64-bit Windows with WSL2. The start bytes for a 32-bit ELF header decode as 16-bit x86 into: 7F 45 JG +45h 4C DEC SP 46 INC SI 01 01 ADD [BX+DI],AX 01 xx ADD ... The first instruction is a jump past the end of the ELF header, unfortunately it's conditional. But we have 9 reserved bytes to continue this code, which is enough to undo the effects of the DEC and ADD instructions and then jump to the same address. I've written a 138 byte "Hello world" that works on Linux, DOS and also CP/M-80 that way. It's possible to have the code that executes under Linux be a small (less than 2K bytes) loader program that creates 16-bit code and data segments and installs a handler for SIGSEGV. It can then jump into the same code that would run under 16-bit DOS, trapping every INT 21h and translating the most important syscalls into their Linux equivalents, kind of like a minimal version of Wine. I have a proof of concept for that, it only handles the "read", "write" and "exit" syscalls, which is enough to write something like rot13 or hexdump. With a lot more work, it could be possible to produce really non-trivial software that runs in such a restricted environment...
- vitiral 3y agoDon't tell jart this or she'll probably add it to cosmopolitan
- actionfromafar 3y agoSame same, but different… How can the syscalls work the same in linux, windows and n my macos?! It’s bananas
- vitiral 3y agoThey don't. APE loads a different syscall table depending on the platform. Or something like that, I don't claim to fully understand the internals