简体   繁体   中英

How to share functions symbols and addresses between projects in C?

I have two distinct projects which are running on the same target. I want my second project to use few functions written in the first project at specific addresses.

To do that I thought I could use the symbol table from the first project in the second but it doesn't work. (I use arm-none-eabi toolchain and -nm on .elf file to generate symbols table).

I know that is possible but how can I do that ?

Well, the brute-force approach will very likely work:

int (*far_function)(int a, int b, int c) = (int(*)(int, int, int)) 0xfeedf00d;

far_function(1, 2, 3);

In other words, just make a function pointer and initialize it using the known address.

If the address isn't well-known (which it won't be if the other application is re-built and you haven't taken steps to "lock" the target function to a particular address), I would instead add meta-data at some fixed address, that contains the pointer. The other application would embed this data, thereby "exporting" the location of the interesting function.

The addresses yielded by nm are the location of the symbols, but on Cortex-M which used the Thumb2 instruction set, those addresses cannot be used directly for jump/call/branch execution - it is necessary to set the LSB of the address to 1 to indicate Thumb mode.

For example:

typedef void (*voidFn_void_t)(void) ;

uint32_t symbol_address = symbolLookup( "myfunction" ) ;

symbol_address |= 1 ;                // Make Thumb mode address
((voidFn_void_t)symbol_address)() ;  // Make call

The called function must even then have no dependencies on the execution environment since it is executing in the environment of the caller, not that of the project it was built in. You may get away with it if the execution environment is be identical but maintaining that may be a problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM