Search code examples
cgccramallocation

Auto-Allocate to a specific RAM Area in GCC/C


Sorry for my english, its a bit hard for me to explain what exactly i would need. I'm making some extra code into existing binarys using the GCC compiler. In this case, its PowerPC, but it should not really matter.

I know, where in the existing binary i have free ram available (i dumped the full RAM to make sure) but i need to define each RAM address manually, currently i am doing it like this:

// #ram.h

//8bit ram
uint8_t*        xx1     = (uint8_t*)    0x807F00;
uint8_t*        xx2     = (uint8_t*)    0x807F01;
//...and so on

// 16bit ram
uint16_t*       xxx1    = (uint16_t*)   0x807F40;
uint16_t*       xxx2    = (uint16_t*)   0x807F42;
//...and so on
    
// 32bit ram
uint32_t*       xxxx1   = (uint32_t*)   0x807FA0;
uint32_t*       xxxx2   = (uint32_t*)   0x807FA4;
//...and so on

And im accessing my variables like this:

void  __attribute__ ((noinline)) silly_demo_function() {
    #include "ram.h"
    
    if (*xxx2>*xx1) {
    *xxx3 = *xxx3 + *xx1; 
    }
    
return; 
}

But this gets really boring, if i want to patch my code into another existing binary, where the location of available/free/unused ram can be fully different, or if im replacing/removing some value in the middle. I am using 8, 16 and 32bit variables.

Is there a way, i can define an area like 0x807F00 to 0x00808FFF, and allocate my variables on the fly, and the compiler will allocate it inside my specific location?


Solution

  • In the meanwhile, i figured it out.

    Its just as easy as defining .data, .bsss and .sbss in the linker directives. 6 Lines of code and its working like a charm.