Running precompiled but not included in the binary code

Hi all,

I'm trying to build a device with an ESP8266 but I run out of space (my ESP has 1 MB flash storage of which I have to reserve about a half for OTA).

I've tried to slim down the code as much as I could but I believe I'm reaching the limit.

I've designed the code in a modular manner so it does not need all the code to run but just one module at a time. For thus, I was thinking if it was possible to download chunks of precompiled code on demand and run it.

I've been reading a lot about this (I even wrote an ELF parser in python to educate myself -after which I realised the binary files for the ESP8266 are not in ELF format-). I found people on internet that categorically states it is not possible. If so, is there a particular reason?

I've also found other documentation related to dynamic linking. Apparently, on any computer you would need to load sections of the binary file into specific executable places of the RAM and then forward the execution to this addresses. I couldn't find any methods similar to "dlopen" for the ESP8266 so I thought like trying something like this:

char* code = (char*) malloc(size(char) * 4);
code[0] = 0x22;   \
code[1] = 0x1C;   | I've compiled a function like "int test() { return 2; }" and
code[2] = 0xF0;   | this was the assembly code
code[3] = 0x0D;  /

int (*func)(void) = (int (*)(void)) code;
func();

But execution crashes and the device restarts. I've also tried all sorts of combinations for little/big endian, alignments for 32 bits, etc but I wasn't able to run such code.

Do you guys have an idea if this is even possible or about what else I could try or look at?

Many thanks.

I've tried to slim down the code as much as I could but I believe I'm reaching the limit.

What have you done towards this objective ?

I've tried to:

  • Abstract as much code as possible in order to call those functions instead of repeating the code
  • Reduce the amount of dependancies to the minimum
  • Reimplement a few dependancies to remove unused code
  • Selected the Basic Ciphers option on Arduino IDE

My nexts steps to try towards this is to have a collection of reusable string that I only need to define once in a single place. But I already know I'll be able to scrap merely a few KB at best with this.

I guess it is also worth mentioning I don't have/need a FS so I'm compiling the code without this in the IDE.

Are you using PROGMEM or the F() macro ?

I'm currently not using them (learnt about them quite recently -douh!-). However, I believe my problem is in the size of the the OTA/firmware itself and using those options would not reduce the size... or would it?