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.

Removing Unused/Dead Functions

Other Parts Discussed in Thread: MSP430F2274

Apologies if this question is more suited for the TI C/C++ Compiler Forum.

I'm trying to prevent unused functions from being linked into my firmware.

I read in the document MSP430 Optimzing C/C++ Compiler V 3.3 (SLAU132E) that you can set the compiler option -gen_func_subsections to achieve this. Unfortunately I'm struggling to find where I set it.

The obvious place is in Project->Properties->C/C++ Build->MSP430 Compiler. There are lots of options you can change here but I can't seem to find the one that sets -gen_func_subsections nor can I find an edit box where I can type it in manually.

Can anyone suggest what I'm doing wrong?

For reference I'm using Code Composer V4.2.3.00004 and the MSP430F2274 CPU.

  • Richard,

    Yeah this one should be in the compiler forum.  No worries, I will move it over there and flag it.

    John

  • Richard,

    Richard Parkin said:

    The obvious place is in Project->Properties->C/C++ Build->MSP430 Compiler. There are lots of options you can change here but I can't seem to find the one that sets -gen_func_subsections nor can I find an edit box where I can type it in manually.


    The option is under Project->Properties->C/C++ Build->MSP430 Compiler->Runtime Model Options called Function Subsections. However, I see that the drop-down list is blank so the option cannot really be activated from here.

    What I noticed though, is that the compiler in CCS 4.2.3, by default, seems to place functions in subsections. You can confirm if this is the case by enabling the option to keep the assembler file (under Project->Properties->C/C++ Build->MSP430 Compiler->Assembler Options, enable the box for "keep the generated assembly language file") and looking for .clink directives in the .asm file. You should also notice that each function within the source file will be in its own section, so there will be directives like:

         .sect   ".text:main"
         .sect   ".text:func1"

    and so on.

    If you see this, then the linker should already be removing ununsed functions. If you do not see this in the asembly file or the unused functions are not being removed, then please provide additional details so we can investigate further.

  • Thanks for the reply.

    I can tell that unused functions are not being removed because if I comment out an unused function the memory requirements decrease.

    I've uploaded an example .asm for one of my source files http://e2e.ti.com/cfs-file.ashx/__key/CommunityServer-Discussions-Components-Files/343/0576.znp_5F00_interface.txt. It appears that each function is in its own section, however its worth noting that unused functions are also present in this file e.g. text.getResetReason.

    Regards

    Richard

     

  • The assembly file will still contain all the functions because at this point it has only been compiled and does not know if those functions are referenced elsewhere in the application. It is only during the link step that unused functions will be removed. Please check the link map file generated by the build (with .map extension, should be in \Debug folder within CCS project). and see if the symbols for ununsed functions are listed in that file. If, for example, getResetReason is a function that is unused it should not be in the map file. If it is, please send us the complete project so we can duplicate this and assist you further.

  • I've checked the .map file and can confirm that the unused functions are not present.

    I think the memory difference is being caused by the strings present in the function.

    e.g.

    char* getResetReason(unsigned char reason)
    {
        switch (reason)
        {
        case 0:     return "Power-up";
        case 1:     return "External";
        case 2:     return "Watch-dog";
        default:    return "Unknown";
        }
    }

    If I modify the function to contain no strings then commenting out the function has no effect on the memory usage.

  • You are correct. Constant strings like the ones you have, do not get eliminated even if the functions which refer to them are removed.

  • Is there a compiler/linker option to prevent this?

  • Richard Parkin said:
    Is there a compiler/linker option to prevent this?

    No.  -George

  • Not sure if the Compiler matters, but I'm using the C6000 DSP tool chain and am having running into this same issue.  Can you confirm that strings of unused functions get linked in to the main functions and there is still no way to prevent this in the C6000 compiler also?

    Thanks,

    Erick

  • If you build for the older COFF ABI, then the strings are included in the final linked executable.  If you use --gen_func_subsections and use --abi=eabi to build for the newer EABI, then the strings are not included in the final linked executable.

    Thanks and regards,

    -George

  • Hi George,

    Thanks for the quick response, however I already am using both of those options in my compiler settings and it still seems to be including strings from unused functions

    ie.  

    void test_function(void) {
        printf("Hello this is a test");
    }

    When looking at the linker map, there is no "test_function" anywhere but in the const. section the data size for the file changes just by changing the string in the printf call.

    I am using the c6000 compiler v7.3.7 w/ Optimizations level set to 3 and Optimize for code size also set to 3.  Was this issue fixed in a later version than the one we are currently using?

    Erick

  • Are you building for EABI by using --abi=eabi?   -George

  • yes, triple checked the settings and it is using the --abi=eabi

  • I can reproduce your problem.  Well, I have reproduced a problem that seems very similar.  Does this look like your issue ...

    Under these conditions ...

    - Two functions are in the same source file
    - One function is called
    - One function is never called
    - Both functions use string literals
    - The string literals are different
    - Build with --abi=eabi --gen_func_subsections

    The compiler puts both string literals in a section named .const:.string.  The code for the function never called is removed.  But the associated string is not removed.  This wastes memory.

    That is what I put in the SDOWP entry SDSCM00046702.  You can track that issue with the SDOWP link below in my signature.

    The assembly generated for such a case might look like ...

    ;******************************************************************************
    ;* STRINGS *
    ;******************************************************************************
    .sect ".const:.string"
    $C$SL1: .string "string never printed",10,0
    $C$SL2: .string "string printed",10,0

    The SL1 string is from a function that is never called.  The SL2 string is from a function that is called.  The code for the never called function is removed.  But since SL1 and SL2 are both in the same section, and SL2 is used, the SL1 string remains.

    Is this the problem you are experiencing?

    Thanks and regards,

    -George

  • Hi George,

    Those conditions are exactly what we see going on.  I didn't necessarily check those conditions in particular, or other conditions, where it would not happen, however those are the conditions of the file we see that happen in.  Thanks for the information, and putting the issue in the SDOWP.

    BTW, when I tried to check the SDOWP link I get this message:

    ClearQuest login of user: This login is forbidden: CRMMD1881E Invalid Credentials: Either the login name or the password is incorrect.

    I get this same message in both Firefox and IE

    Erick

  • Erick Higa said:
    ClearQuest login of user: This login is forbidden: CRMMD1881E Invalid Credentials: Either the login name or the password is incorrect.

    Things like this are usually temporary.  It worked for me just now.  Please try again.

    Thanks and regards,

    -George