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/RM48L952: unused global version string removed by compiler, how to keep it?

Part Number: RM48L952

Tool/software: TI C/C++ Compiler

Hello there,

I want to use strings (the linux command) to obtain version and git information from an executable.
With GCC it works when adding the following lines to the sourcecode.

main.c:

#include <stdint.h>

const char* version_string = "#_#_ Version info - version 0.1 built on (2017-10-09 11:19:00) _#_#";

int main(void){

/*

 * SOME CODE HERE.

 */

}

I can then use:

$ strings my_executable.out | grep "#_#"

To obtain the version string information.

However, when i compile the same source file using the TI compiler the string gets removed in the object file. 
When I use the variable in some dummy function the compiler does not remove it and i can obtain it using strings, however we do not want dead code.

I have tried adding volatile, static, __attribute__((used)) but none of these give any difference.

I am using these compile flags (from inside CCS):

-mv7R4 --code_state=32 --float_support=VFPv3D16 -me -Ooff --opt_for_speed=1 --include_path="${CG_TOOL_ROOT}/include" --include_path="${workspace_loc:/${ProjName}/include}" -g --symdebug:dwarf_version=3 --c99 --printf_support=minimal --diag_warning=225 --diag_wrap=off --display_error_number --enum_type=packed --abi=eabi 

These are the link options, however as the string is also not present in the object file I do not assume that the linker causes any troubles.

-m"${ProjName}.map" --heap_size=0x800 --stack_size=0x800 -i"${CG_TOOL_ROOT}/lib" -i"${CG_TOOL_ROOT}/include" --reread_libs --diag_wrap=off --display_error_number --warn_sections --xml_link_info="${ProjName}_linkInfo.xml" --rom_model --unused_section_elimination=off 

I am using version v16.9.0.LTS.

So now the question is can I add a compile/link flag to prevent the compiler/linker from removing this variable?

Thanks in advance :)
Karel

  • K De Coster said:
    I have tried adding volatile, static, __attribute__((used)) but none of these give any difference.

    __attribute__((used)) should have done it.  Because it doesn't, I filed CODEGEN-3793 in the SDOWP system.  You are welcome to follow it with the SDOWP link below in my signature.

    K De Coster said:
    can I add a compile/link flag to prevent the compiler/linker from removing this variable?

    Use #pragma RETAIN.  Please read more about it in the ARM compiler manual.

    Thanks and regards,

    -George

  • Hello George,

    Thanks for your fast response.
    Turns out adding __attribute__((used)) does work, i had tried it in combination with static (static const char * version_string __attribute__((used)) = "#_#...#_#"';). It appears that the combination of static and __attribute__((used)) makes it disappear. I do not quite understand how these 2 properties contradict each other. However my issue is resolved.
    Solution:

    const char* version __attribute__((used)) = "#_# v0.1 #_#";

    Turns out the compile flag -Ooff is not even needed.
    Linking flag --unused_section_elimination=off was needed, otherwise the linker removes it.

    Kind regards and many thanks,


    Karel
  • K De Coster said:
    Linking flag --unused_section_elimination=off was needed

    If everything is working as it should, then you should not need to take this step.  Further, using this option probably causes other sections, which should be removed, to be kept, thereby wasting memory.  

    Please try using #pragma RETAIN as I described earlier.  That alone should fix it.

    Thanks and regards,

    -George

  • You are correct, the combination of __attribute__((used)) and #pragma RETAIN also fixes the isue. This is how it written down now:

    #pragma RETAIN(version)
    const char const * version __attribute__((used)) = "#_# _ version _#_#";

    Thanks again :)