I appreciate the replies!
@nimishFirst of all, this is different from an unpacker. Unpacker requires to decompress the program into memory, so you end up using more total memory than the original program. Here, we actually use only C + D + I bytes (where C is the size of the compressed code, D is the size of the decompressor, and I is some additional small constant amount of RAM the interpreter needs). An unpacker would use something like C + O (where O is the original program).
Regarding VMs, it kind of works like that, but then there is another layer of generic compression. A VM works with a bytecode, but the system I described works compressed bytecode. It is much smaller (in my tests, rv32ec instructions can be almost halved in size!), and theoretically can work with any compression (lz, grammar, entropy, etc).
@SiwastajaI agree with your points. This is pretty much the very last resort, when you have a program that is too large, you cannot make it smaller, and you cannot upgrade hw. In that specific, (unrealistic?) scenario this is the only way to run that program.
@gfI should have probably described the architecture eariler.
The whole pipeline starts with 2 object files,: guest and native. Guest represent code that we want to compress, and it has to be compiled for RV32EC. Native code represents code that we want to keep uncompressed (for performance reasons, or as external code we don't really want to compress), and it can be in any ISA. I have an idea of how to make native architecture different from guest, but for this time, assume it is RV32I.
From high-level perspective, we will link guest and native code in a way which will put all guest code in an unsused segment of memory (like 0xfff00000..0xfffffff). This way, whenever we read an instruction that loads / calls something, we can check whether that something belongs to guest (has the 0xfff... prefix) or not. If it belongs to guest, we use the decompressor to fetch the thing. Suppose we want to read an integer that is embedded in a .guest.rodata section at 0xfff00004. Then, knowing that the compressed section begins at 0xfff00000, we can ask the decompressor to decode the word at position 0x4..0x8 in the compressed payload.
After we do this linkage, we separate guest code from native code. Next, we find addressable symbols: symbols in guest section which are taken an address of in any part of the code. Why? Because suppose a function accepts a pointer to another function. Compressed code can't really be accessed by pointers, so we need to generate stubs for each symbol that needs to have a static address. We scan relocations to find such symbols, and then emit two things:
1. A section with stubs, where each stub (in pseudocode) looks like:
stub_<original_symbol>:
jal x31, <symbol that calls the interpreter machinery>
Here, we have a stub_<symbol> for each symbol which will jump directly to the function which handles native -> guest calls. It will see x31, subtract it from the beginning of the stubs section (to get the index of the symbol this stub represents).The handler will then read the position of that symbol in the compressed payload, and simply run the interpreter from that place.
2. A table which is used in previous point, and contains offsets into the compressed payload for each stub:
.word 0x00010dfc /* slot 176: <symbol 1> */
.word 0x000088fa /* slot 180: <symbol 2 */
...
After we have this informations, we link again, but this time we replace each addressable symbol <symbol> in the entire program with its stub_<symbol>. For this I used --wrap which does exactly that with the linker.
Having this done, we can attach the interpreter, decompressor, link everything once again (without linking the 0xfff... section, as it is now compressed and stored somewhere the interpreter knows how to access it) and it's ready!
====
This is actually just the very, very shallow description of how it works. It is much more complicated, there are many optimizations done. For example, I have a profiler which reorders functions in the compressed payload in a way which makes hotter code be faster to decompress. I also implemented another profiler which can shuffle symbols around such that 95% of all cold code is placed in guest, and 5% hot code is placed in native code. This helps improve the performance 2-3 times, but unfortunately the overhead of compressed code is soooo large I need the hot code to be about 0.5% for this system to actually run at near-native speeds...
I have a public version of this which is very outdated, but is essentially what i described above:
https://github.com/lis05/sseocb. I didn't really know whether there is a point in making the whole thing public, but if anyone here would be interested to take a look, I can update the public repo and hopefully write some meaningful description. Unfortunately, I only had about 3 months to work on it, so its heavily vibecoded, and I want to make a clean rewrite (which is pretty much the reason I made this post, to see if there is a point in spending any more time on such a bizzare idea)