This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

A question about data placement in IAR

Hi,

 

In IAR, I can use #pragma location directive to place a global (or static) variable at absolute address, for example,

 

#pragma location="MYSEG"

const int  var = 10;

 

Then, var = 10 should be located in segment "MYSEG".

However, IAR's compiler won't put var = 10 into "MYSEG", if it finds that the var is not used directly.

So, does anyone else know a method to force the compiler to put the variable into the memory regardless whether the variable is used or not.

 

Thanks

Liu

 

  • Try using the __root directive.

  • Try something like:

    ORG address ;start address for variable reservation in RAM

    var DS x ;where X is the number of byte reserved for variable var

     

  • milanqingren said:
    So, does anyone else know a method to force the compiler to put the variable into the memory regardless whether the variable is used or not

    Yes. Several:

    1) use it. If you don't use it, why is it there? Unused/unreferenced things won't make it into the build.
    If you do some fancy things to use it in a way that not even the compiler will notice that you are, then you should question your approach and programming style. Remember: it will end up anywhe  ein the MYSEG, not at a fixed address (whille you can assume that it will end up at the start of the segment if it is the only member, there's nothing that will ensure this, so it is a risky assumption).

    2) use a keep statement in the linker file. So the linker will not throw it away if it doesn't see any reference

    3) group it with other datat that IS used. Normally, teh linker will look at each data block in a whole. If everything in a block is referecned, the whole block is kept. But this is also a risky assumption about how compiler and linker will act on your code.

    milanqingren said:
    However, IAR's compiler won't put var = 10 into "MYSEG", if it finds that the var is not used directly.

    No. the compiler generates an entry to tell the linker that there is a variable with content 10 in the MYSEG segment. But the linker detects tha none of the code partswhich are linked will ever reference it, so it discards it.

    Same happens if you have code in a .c file and compile it, but you never call a function from this file. Then all the code in this file won't be linked.
    It's the way linking works and the way how libraries are possible at all.

  •  

    EW430_CompilerReference:

     

    __root

    Syntax: Follows the generic syntax rules for object attributes, see Object attributes, page 191.

     

    Description: A function or variable with the __root attribute is kept whether or not it is referenced from the rest of the application, provided its module is included. Program modules are always included and library modules are only included if needed.

    Example: __root int myarray[10];

    See also: To read more about modules, segments, and the link process, see the IAR Linker and Library Tools Reference Guide.

     

     

     

  • darkwzrd said:
    __root

    Well, it will work. It seems to simply add a reference count for the linker, so the linker thinks it is referenced.

    it is, however, IAR specific and makes the code nonportable. Not using elements which are not referenced is the better alternative :)

  • I want to have a similar functionality but in my CCS application .c code. Can anyone let me know how I can do this??

     

    I want to place a password at a fixed location in flash and I want to do this from inside C code.

     

    Thanks.

**Attention** This is a public forum