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.

Compiler: TI compiler & linker - retain unused variables

Tool/software: TI C/C++ Compiler

Hello,

I am trying to make the compiler & linker retain UNUSED sections/variables.

It works if i use the #pragma RETAIN in the source file:

#pragma DATA_SECTION (testVar,".sec1")
#pragma RETAIN(testVar)

unsigned int testVar;

But i am trying to make it work using the __attribute__ syntax in order for the sources to be more compatible with the gnu tools

__attribute__((section (".sec1"))) __attribute__((used)) unsigned int testVar;

and in the linker i add : --retain ('.sec1*')

the section attribute works but the used attribute + linker retain does not seem to be working and i still must used the #pargma RETAIN.

Is there a reason why attribute((used)) + linker retain is not working and the variable is still thrown out ? any way to achieve it without having to use the #pragma RETAIN?

Thanks

Guy

  • We have seen similar issues with the used attribute.  For example, here is one forum thread.  We considered changing how the used attribute works.  But, since yet other users may depend on current behavior, we decided against that change.  Instead, we introduced the attribute retain, which does exactly the same thing as pragma retain.  To solve your problem, please write __attribute__((retain)) instead of __attribute__((used)) .

    Thanks and regards,

    -George

  • Hello , Thanks.
    I have tried, per you advice, to use __attribute__((retain)) but is also did NOT work and
    (i also kept the --retain('.sec*) in the linker script).
    only the #pragma RETAIN seems to hold - any suggestions?

    I am using the following tools versions :
    TI v8.1.0 (for DSP)
    arp32_1.0.7 (for EVE)

    Also, another issue is that since so far i was not able to have an attribute that can replace the pragma RETAIN, i have tried using

    #ifdef __GNUC to detect a gnu compiler. unfortunately it appears that this macro is also defined for the EVE (i am using the gnu extensions but accroding to the EVE's compiler manual it should define _ _TI_GNU_ATTRIBUTE_SUPPORT_ _ and not __GNUC__)

    not sure why i get the __GNUC__ defined for EVE.

    Can you suggest a compiler macro that i can use safely to distinguish between TI tools (including EVE) vs gnu tools?

    It will be better if you'll be able to suggests proper attributes (to0 replace the retain pragma) that can be shared among TI and gnu as i rather not having #ifdefs over all of our common files for all needed variables just to distinguish between GNU and TI.

    B.T.W

    You mentioned that instead of "fixing" the used attribute you created a retain attribute (even though not valid for my TI tools) since you wanted to maintain backward compatibility but as far as i can tell the used attribute currently does not seem to do anything - am i missing something (does it do something i am not aware of)?

    Thanks

    Guy


    Thanks
    Guy

  • Guy Mardiks said:
    I have tried, per you advice, to use __attribute__((retain)) but is also did NOT work

    This attribute is introduced in C6000 compiler version 8.3.0.  I'm sorry I didn't mention that in my previous post.

    Guy Mardiks said:
    I am using the following tools versions :
    TI v8.1.0 (for DSP)

    If you cannot upgrade to 8.3.0, then your only solution is #pragma RETAIN.

    Guy Mardiks said:

    i have tried using

    #ifdef __GNUC to detect a gnu compiler.

    This problem is fixed in later versions of the C6000 compiler.  The fix has not been applied to the EVE compiler.  With the EVE compiler, if you use the switch --gcc, then __GNUC__ is predefined.  Try removing --gcc.  It would fix this problem.  I'm not sure if it will create yet other problems.  But I don't think so.

    Guy Mardiks said:
    You mentioned that instead of "fixing" the used attribute you created a retain attribute (even though not valid for my TI tools) since you wanted to maintain backward compatibility but as far as i can tell the used attribute currently does not seem to do anything - am i missing something (does it do something i am not aware of)?

    The behavior of the attribute used is described in the GCC documentation.  In our view, it is not clear and well defined.  In particular, we are concerned that if somehow changed what it does, we would cause problems for customers who currently have it in their source code.  Rather than risk that, we introduced the attribute retain.

    Thanks and regards,

    -George