1

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 ?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
LOSnel
  • 161
  • 1
  • 1
  • 8
  • 1
    *... but it doesn't work...* That explains the problem completely. – Ajay Brahmakshatriya Apr 13 '17 at 10:20
  • I am going to assume you are using Cortex-M since that is what CooCox supports - not the most helpful tag - target information is more useful than development platform. – Clifford Apr 13 '17 at 13:28
  • Show the code making the call (the code that does not work), indicate the address being used, tag or state the target platform. You'll get a quicker, more accurate and more relevant answer that way. – Clifford Apr 13 '17 at 13:47
  • I'm sorry, Is this question trying to ask "How do I factor out shared code between 2 projects into a shared library and then link both projects to that library?" ... or is it some weird arm/compiler specific thing? – technosaurus Apr 13 '17 at 21:52

2 Answers2

1

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.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • Did you use `0xfeedf00d` merely because it was amusing or because being an odd value it is a valid ARM-Thumb2 branch address? ;-) – Clifford Apr 13 '17 at 13:50
  • @Clifford Mainly the former, I thought about somebody complaining about it being odd and prepared the Thumb2 defense. :) Didn't know OP's platform. – unwind Apr 13 '17 at 14:17
  • @unwind thanks for quick answer, it seems to work as I expected.My platform is Cortex-M3 (STM32L151) – LOSnel Apr 14 '17 at 10:01
0

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.

Clifford
  • 88,407
  • 13
  • 85
  • 165