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.

Using #pragma clang section Overrules --unused_section_elimination

Hello,

Normally the linker throws out functions which are not called. --unused_section_elimination = on (default)

However, if I specify a section that is used for both called foo1() and not called functions foo2(), both functions are linked regardless.

I guess you can say that the #pragma section is used so will not be removed but equally unused functions are still removed when the default section (.text) is used.

<Question>

Is there a way to prevent linking of foo2() with compiler options without changing the code in the second case please? 

The background is that this is a problem for AUTOSAR Memory Mapping system including the TI provided MCAL (e.g. Adc_MemMap.h) where scope for modifying code is limited. Because all functions share a common #pragma section, all unused functions are linked thus inflating the program size unnecessarily.

  • Is there a way to prevent linking of foo2() with compiler options without changing the code in the second case please? 

    Unfortunately, no.  

    Details ... Please read the first part of the article Linker Command File Primer.  Focus on learning the terms input section and output section.  The granularity of control of the linker feature for removing functions never called (or data objects never referenced) is the input section.  Either a complete input section is part of the build, or it isn't.  If a single input section contains two functions, the linker keeps the entire input section if either function is called.  It is impossible for the linker to somehow carve out the function that isn't called.

    Thanks and regards,

    -George