RA6M5 gcc variable allocation address order


if I declare some variables, for example:

int v1;

int v2;

int v3;

then are allocated in memory in reverse order, for example in the map file:

0x20001100                v3

0x20001104                v2

0x20001108                v1

There is the possibility to indicate to the linker to allocate them in the expected crescent order? I.e.:

0x20001100                v1

0x20001104                v2

0x20001108                v3

  • Hello,

    The first variables will be place in a lower addresses in stack.

    Instead of letting the compiler place the variable into addresses, you can use the address of one variable as base address and allocate the rest before or after this address. 


  • Hello,

    this is not easy to apply, because I need to make the porting of a lot a sw that expects to have the variables, thousands of variables, allocated in ascending order of addresses. I think this is a very strange philosophy for the gcc linker to allocate the variables...

    Also I thinked to declare a very big array and then the single variables declared as define to a specifica array, for example:

    int array[2000];

    #define v1 array[100]

    #define v2 array[101]

    #define v3 array[102]

    but this preclude lot of debug possibility, and is prone to many mastakes.

    Also may I add the attribute:

    int v1 __attribute__ ((address (0x20001100)));

    int v2 __attribute__ ((address (0x20001104)));

    int v3 __attribute__ ((address (0x20001108)));

    but is not a good solution...

    Best regards

  • Hello,

    the simplest final solution is to disable the optimization in a file where all the variables needs to be allocated (example: memory.c) in direct order.

    To do that, add in the file memory.c the row:

    #pragma GCC optimize ("O0")

    or in e2studio in Project Explorer right click on the file memory.c, select Properties > C/C++ Build / Settings ; in Tool Settings \ Optimization add in the "Other optimization flags" the switch (without double quotes) "-O0".

    The default optimization (if no explicit settings are defined) is -O2; this allocates the variables in reversed order.

    This GCC version doesn't support the attributes "address" or "location" or "at", i.e.:

    int v1 _attribute__ ((address (0x20001100)));

    int v1 _attribute__ ((location (0x20001100)));

    int v1 _attribute__ ((at (0x20001100)));

    There is in this version the possibility to assign the address using __attribute__?

    This works, but is not useful:

    #define v1 *(int *)(0x20001100)

    #define v2 *(int *)(0x20001104)

    #define v3 *(int *)(0x20001108)

    Then using:

    v1 = 1234;

    int foo = v1;

    Is also possible to declare the variables in inline assembler, but it is insane...

    Best regards