2

I am implementing a C function which will be copied to an other memory location during run time.

I have avoided function calls inside it, only using pure constant inside it. When I examine the assembly output, the constant access is translated into PC relative addressing instructions. However the relative address (literal pool) is outside the function. (Looks like they are generated in ramdom places).

I know in ARM assembly there is a .ltorg directive which can localize these literal pool, is there a method in C to do the same thing?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Eric Sun
  • 777
  • 6
  • 20
  • 1
    What are you trying to achieve ? – Jabberwocky Jan 08 '16 at 10:08
  • 1
    stackoverflow is not a tutorial site! however, place the variables/literals in a unique section, then in a linker command file place the unique section at a known location (probably in flash). The compiler should then generate re-locatable address references to those literals and the linker will fix the addresses. – user3629249 Jan 08 '16 at 12:06
  • What about `-mno-pic-data-is-text-relative` from https://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html – auselen Jan 08 '16 at 12:22
  • I think this was asked already; I am too lazy to find the dup. I have used a 2nd dummy function to do this. For example, https://goo.gl/RkKWC7. However, the method is somewhat brittle. If you enable *lto* or even update compiler versions, something may change. Definitely the `attribute((section"xxx"))` is worth looking into, but I never seem to get it right. The best is to use a linker script and put the relocated code in a separate file. `.ltorg` always goes in the *text* segment. – artless noise Jan 08 '16 at 14:07
  • Possible duplicate of [How to run code from RAM on ARM architecture](http://stackoverflow.com/questions/15137214/how-to-run-code-from-ram-on-arm-architecture) – artless noise Jan 08 '16 at 14:30
  • @user3629249 Thanks for your guide. I just don't know if it is applicable in high level languages. I have successfully implement it using compiler specific "#pragma" to make the compile output into a specific section. And this makes all literal pools location controllable. I don't modify the linker command file, I think that only affect linkage process, and I only cares about compile stage at this moment. – Eric Sun Jan 12 '16 at 02:11
  • put the to be relocatable code in its own function then use the object to relocate, or make it its own complete standalone binary. – old_timer Jan 12 '16 at 02:28
  • The compile step does not care where the literals are located. The linker fixes all relocatable addresses, so the linker makes all the decisions about actual addresees – user3629249 Jan 12 '16 at 12:19

0 Answers0