Loading a binary and executing it within the mbed(dynamically)

06 Mar 2012

Hello, I like messing around with some of the odder parts of mbed (mostly because I like messing around with my only ARM processor :) ). Anyway, one of the things I tried doing is loading a file from LocalFileSystem into RAM and then using a function pointer like so:

uint8_t *ram=malloc(filesize);
//load file into ram
void (*MyFunction)(void);
MyFunction=ram;
MyFunction();

However, it seems to just crash after executing MyFunction. I'm not experienced in ARM assembly, but I understand assembly in general(I'd consider myself an expert with x86 assembly). Anyway, I'm not sure what kind of crt0 is put in by the online compiler, but basically what I am doing is making loader.bin that does this and then example.bin that is just a test program that blinks LEDs.

Has anyone done this before and know what else is required to get this to work and for it to return to the "loader.bin" code?

06 Mar 2012

Jordan,

How did you build your MyFunction() binary? The biggest issue I see with doing what you have attempted here is that the code is probably not compiled to be position independent so you can't just load the code into any arbitrary memory location. If you were writing the code in ARM assembler yourself then obviously you could make all of your memory references PC relative to work around such problems but by default I believe that both the mbed online compiler and GCC build position 'dependent' code.

-Adam

PS: A normal bin file will start with the interrupt vector table and won't contain actual executable code.

07 Mar 2012

Ah I see. So I basically can't use mbed's online compiler then? It should be possible though with a gcc cross compiler then to use the PIC directive and compiling a freestanding directive?

10 Mar 2015

HI guys Great Question...Have any one succeeded in doing like this..using GCC

-deleted-
16 Mar 2015

I would also be interested in this type of functionality for dynamically loading compiled code at runtime, especially for FRDM-K64F and FRDM-K22F. Perhaps the FreescaleIAP library would be able to load the compiled function as a freestanding directive into flash memory, where the code can then be executed? See: http://developer.mbed.org/users/Sissors/code/FreescaleIAP/

16 Mar 2015

Position independent code (& data?) is one approach. Another perspective would be that you could use a slightly more capable program linker/loader/launcher that fixes the position-dependent references and ties to system services before running the loaded module. For example, consider an ARM adaption of CP/M68K (w/ 'C'-based sources), UZI, or FreeDOS. Since CP/M(2.2) can be run in an emulator on mbed, a 'native' CP/M port should be feasible, too.